我知道,这个问题已经被问过多次了,我已经尝试了许多this,this,this的答案,但是没有任何帮助。
我这里有JSON字符串
"[
{\"heading\":\"Bike Services\",
\"content\":[
{\"title\":\"General Service\",\"icon\":\"bike_service_img\"},
{\"title\":\"Premium Bike Service\",\"icon\":\"bike_service_img\"},
{\"title\":\"Repair Job\",\"icon\":\"bike_service_img\"},
{\"title\":\"Bike Ceramic Coating\",\"icon\":\"bike_service_img\"}
]
},
{\"heading\":\"Bike Service Centre Near You\",
\"content\":[
{\"title\":\"Bike Shop Name one\",\"icon\":\"bike_service_centre_img\"},
{\"title\":\"Bike Shop Name two\",\"icon\":\"bike_service_centre_img\"},
{\"title\":\"Bike Shop Name three\",\"icon\":\"bike_service_centre_img\"},
{\"title\":\"Bike Shop Name four\",\"icon\":\"bike_service_centre_img\"},
{\"title\":\"Bike Shop Name five\",\"icon\":\"bike_service_centre_img\"},
{\"title\":\"Bike Shop Name six\",\"icon\":\"bike_service_centre_img\"}
]
},
{\"heading\":\"Car Services\",
\"content\":[
{\"title\":\"Express Service\",\"icon\":\"car_service_img\"},
{\"title\":\"Dent/Scratch Removal\",\"icon\":\"car_service_img\"},
{\"title\":\"Interior Detailing\",\"icon\":\"car_service_img\"},
{\"title\":\"Oil Change Package\",\"icon\":\"car_service_img\"},
{\"title\":\"Complete Car Spa\",\"icon\":\"car_service_img\"},
{\"title\":\"AC Service\",\"icon\":\"car_service_img\"}
]
},
{\"heading\":\"Car Service Centre Near You\",
\"content\":[
{\"title\":\"Car Shop Name one\",\"icon\":\"car_service_centre_img\"},
{\"title\":\"Car Shop Name two\",\"icon\":\"car_service_centre_img\"},
{\"title\":\"Car Shop Name three\",\"icon\":\"car_service_centre_img\"},
{\"title\":\"Car Shop Name four\",\"icon\":\"car_service_centre_img\"},
{\"title\":\"Car Shop Name five\",\"icon\":\"car_service_centre_img\"},
{\"title\":\"Car Shop Name six\",\"icon\":\"car_service_centre_img\"}
]
}
]"
还有POJO类ContainerItems
public class ContainerItems {
String heading;
List<ContainerContents> content;
public String getHeading() {
return heading;
}
public void setHeading(String heading) {
this.heading = heading;
}
public List<ContainerContents> getContent() {
return content;
}
public void setContent(ArrayList<ContainerContents> content) {
this.content = content;
}
}
和ContainerContents.java
public class ContainerContents {
private String title;
private String icon;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getIcon() {
return icon;
}
public void setIcon(String icon) {
this.icon = icon;
}
}
通过Gson
库进行解析,如下所示
Gson gson = new Gson();
List<ContainerItems> containerItemList = gson.fromJson(mainContainerItems, new TypeToken<List<ContainerItems>>() {}.getType());
但是出现错误
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 2 path $
at com.google.gson.Gson.fromJson(Gson.java:939)
at com.google.gson.Gson.fromJson(Gson.java:892)
at com.google.gson.Gson.fromJson(Gson.java:841)
at com.sam.service.fragments.HomeFragment.loadContainerItems(HomeFragment.java:149)
at com.sam.service.fragments.HomeFragment.onCreateView(HomeFragment.java:133)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2346)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1428)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1759)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1827)
at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:797)
at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2596)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2383)
at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2338)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2245)
at android.support.v4.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3248)
at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:3200)
at android.support.v4.app.FragmentController.dispatchActivityCreated(FragmentController.java:195)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:597)
at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:177)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1391)
at android.app.Activity.performStart(Activity.java:7157)
at android.app.ActivityThread.handleStartActivity(ActivityThread.java:2938)
at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:180)
at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:165)
at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:142)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:70)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1809)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6680)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 2 path $
at com.google.gson.stream.JsonReader.beginArray(JsonReader.java:350)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:80)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:61)
我无法理解这里出了什么问题。请在这里帮助我。
答案 0 :(得分:0)
您的JSON以"
开始和结束,因此它是一个String而不是数组。首先从JSON中删除""
,然后尝试将其解析为数组。
答案 1 :(得分:0)
您是否验证json
格式?我已经重新设计如下,然后它可以工作。
[
{
"heading": "Bike Services",
"content": [
{
"title": "General Service",
"icon": "bike_service_img"
},
{
"title": "Premium Bike Service",
"icon": "bike_service_img"
},
{
"title": "Repair Job",
"icon": "bike_service_img"
},
{
"title": "Bike Ceramic Coating",
"icon": "bike_service_img"
}
]
},
{
"heading": "Bike Service Centre Near You",
"content": [
{
"title": "Bike Shop Name one",
"icon": "bike_service_centre_img"
},
{
"title": "Bike Shop Name two",
"icon": "bike_service_centre_img"
},
{
"title": "Bike Shop Name three",
"icon": "bike_service_centre_img"
},
{
"title": "Bike Shop Name four",
"icon": "bike_service_centre_img"
},
{
"title": "Bike Shop Name five",
"icon": "bike_service_centre_img"
},
{
"title": "Bike Shop Name six",
"icon": "bike_service_centre_img"
}
]
},
{
"heading": "Car Services",
"content": [
{
"title": "Express Service",
"icon": "car_service_img"
},
{
"title": "Dent/Scratch Removal",
"icon": "car_service_img"
},
{
"title": "Interior Detailing",
"icon": "car_service_img"
},
{
"title": "Oil Change Package",
"icon": "car_service_img"
},
{
"title": "Complete Car Spa",
"icon": "car_service_img"
},
{
"title": "AC Service",
"icon": "car_service_img"
}
]
},
{
"heading": "Car Service Centre Near You",
"content": [
{
"title": "Car Shop Name one",
"icon": "car_service_centre_img"
},
{
"title": "Car Shop Name two",
"icon": "car_service_centre_img"
},
{
"title": "Car Shop Name three",
"icon": "car_service_centre_img"
},
{
"title": "Car Shop Name four",
"icon": "car_service_centre_img"
},
{
"title": "Car Shop Name five",
"icon": "car_service_centre_img"
},
{
"title": "Car Shop Name six",
"icon": "car_service_centre_img"
}
]
}
]
答案 2 :(得分:0)
您的json不是JSON对象,而是JSON数组。因此,您要反序列化JSON的POJO应该包含List<ContainerItems>
而不是类ContainerItems
本身的列表。这意味着您必须使用List<ContainerItems>
创建另一个类,在这种情况下,您必须给此变量起一个名字(比如说myvariable
),因此您可能必须将输入的json转换为{{1} }
希望这会有所帮助!
答案 3 :(得分:0)
您对此JSON
有两个问题:
\"
"[...]"
在将有效负载进行反序列化之前,您需要消除两个问题:
String unescapeJson = StringEscapeUtils.unescapeJson(json);
String striped = StringUtils.strip(unescapeJson, "\"");
Type type = new TypeToken<List<ContainerItems>>() {
}.getType();
List<ContainerItems> containerItemList = gson.fromJson(striped, type);
其中:
StringEscapeUtils
来自org.apache.commons.text.StringEscapeUtils
StringUtils
来自org.apache.commons.lang3.StringUtils
另请参阅:
答案 4 :(得分:0)
在使用材料库时,我遇到了类似的问题。对我有用的是: 重构->清理项目