我有一个json:
{
"mainObject": "firstMainObject",
"mainObjectInfo": {
"firstInfo": "1",
"secondInfo": "2"
},
"values": [
{
"valueName": "111",
"valueList": {
"details": [
"111FirstDetails",
"111SecondDetails"
],
"valueDescription": "desc1",
"detailsInfo": "111detailsInfo"
},
"endValue": "11End"
},
{
"valueName": "222",
"valueList": {
"details": [
"222FirstDetails",
"222SecondDetails"
],
"valueDescription": "desc2",
"detailsInfo": "222detailsInfo"
},
"endValue": "22End"
},
{
"valueName": "333",
"valueList": {
"details": [
"333FirstDetails",
"333SecondDetails"
],
"valueDescription": "desc3",
"detailsInfo": "333detailsInfo"
},
"endValue": "33End"
}
],
"lastInfo": "ok"
}
我想从valueDescription
中提取detailsInfo
和valueList
值的列表。
我创建了一个类:
public class MyObject {
@SerializedName("valueDescription")
String valueDescription;
@SerializedName("detailsInfo")
String detailsInfo;
}
然后使用:
Gson gson = new Gson();
List<MyObject> myObject = gson.fromJson(myJson,new TypeToken<ArrayList<MyObject>>(){}.getType());
但出现问题并且出现错误:
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $
at com.google.gson.Gson.fromJson(Gson.java:900)
at com.google.gson.Gson.fromJson(Gson.java:853)
at com.google.gson.Gson.fromJson(Gson.java:802)
at com.package.MyClass.myMethod(MyClass.java:95)
at com.package.MyClass.myMethod(MyClass.java:33)
at com.atlassian.stash.internal.hook.repository.DefaultRepositoryHookService.postUpdate(DefaultRepositoryHookService.java:788)
at com.atlassian.stash.internal.hook.repository.DefaultRepositoryHookService.doPostUpdate(DefaultRepositoryHookService.java:595)
at com.atlassian.stash.internal.hook.repository.DefaultRepositoryHookService.postUpdate(DefaultRepositoryHookService.java:362)
at com.atlassian.stash.internal.hook.repository.CoreRefChangeEventToRepositoryHookAdapter.onRefsChanged(CoreRefChangeEventToRepositoryHookAdapter.java:55)
at com.atlassian.event.internal.SingleParameterMethodListenerInvoker.invoke(SingleParameterMethodListenerInvoker.java:40)
at com.atlassian.stash.internal.event.AsyncBatchingInvokersTransformer$AsyncInvokerBatch.invoke(AsyncBatchingInvokersTransformer.java:109)
at com.atlassian.event.internal.AsynchronousAbleEventDispatcher$1$1.run(AsynchronousAbleEventDispatcher.java:38)
at com.atlassian.sal.core.executor.ThreadLocalDelegateRunnable.run(ThreadLocalDelegateRunnable.java:34)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at com.atlassian.stash.internal.event.EventThreadFactory.lambda$createThread$0(EventThreadFactory.java:24)
at java.lang.Thread.run(Thread.java:745)
... 31 frames trimmed
Caused by: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT 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)
at com.google.gson.Gson.fromJson(Gson.java:888)
... 16 common frames omitted
我想要一个MyObject
对象列表,如:
1)由valueDescription
= desc1
和detailsInfo
= 111detailsInfo
2)由valueDescription
= desc2
和detailsInfo
= 222detailsInfo
3)由valueDescription
= desc3
和detailsInfo
= 333detailsInfo
如何从我的json中提取这个值?
答案 0 :(得分:1)
您正在尝试将JSON对象解析为JSON数组。
您正在寻找的是这样的:
用于解析整个对象:
public class WrapperObject {
@SerializedName("values")
List<ValueObject> values;
public List<ValueObject> getValues(){
return values;
}
}
属性包装器:
public class ValueObject{
@SerializedName("valueList")
MyObject valueList;
public MyObject getValueList(){
return valueList;
}
}
必填信息:
public class MyObject{
@SerializedName("valueDescription")
String valueDescription;
@SerializedName("detailsInfo")
String detailsInfo;
}
解析:
WrapperObject wrapperObject = new Gson().fromJson(myJson, WrapperObject.class);