您好我有一个复杂的JSon对象列表。我想将其转换为对象列表。 但我得到的是例外情况。
[
{
"pas": "TEST1",
"policyNumber": "1009",
"schemeName": "BB",
"policyStatus": "ACTIVE",
"productCode": "CC",
"effectiveDate": {
"startDate": "2016-01-27T22:10:18Z",
"endDate": ""
}
},
{
"pas": "TEST2",
"policyNumber": "10002",
"schemeName": "AA",
"policyStatus": "ACTIVE",
"productCode": "AA",
"effectiveDate": {
"startDate": "2016-01-27T22:10:18Z",
"endDate": ""
}
}
]
PFB我的POJO班
@SerializedName("pas")
private String pas;
@SerializedName("policyNumber")
private String policyNumber;
@SerializedName("schemeName")
private String schemeName;
@SerializedName("policyStatus")
private String policyStatus;
@SerializedName("productCode")
private String productCode;
@SerializedName("totalSavings")
private BigDecimal totalSavings;
@SerializedName("investmentReturn")
private BigDecimal investmentReturn;
@SerializedName("effectiveDate")
private EffectiveDate effectiveDate;
EffectiveDate.java
@SerializedName("startDate")
private DateTime startDate;
@SerializedName("endDate")
private DateTime endDate;
我使用下面的代码将其转换为对象列表
policies = new ArrayList<Policy>();
JsonParser parser = new JsonParser();
JsonElement jsonElement = parser.parse(new FileReader("./src/test/resources/" + "sample.json"));
Type listType = new TypeToken<List<Policy>>(){}.getType();
List<Policy> policyList = new Gson().fromJson(jsonElement, listType);
但是我得到了以下例外:
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:200)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:103)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:196)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:103)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:196)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:40)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:81)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:60)
at com.google.gson.Gson.fromJson(Gson.java:810)
at com.google.gson.Gson.fromJson(Gson.java:875)
at com.zurich.fn.consolidation.common.AbstractCommon.initPolicyList(AbstractCommon.java:168)
at com.zurich.fn.consolidation.handler.RetrievePensionsEndpointHandlerImplTest.setup(RetrievePensionsEndpointHandlerImplTest.java:53)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING
at com.google.gson.internal.bind.JsonTreeReader.expect(JsonTreeReader.java:139)
at com.google.gson.internal.bind.JsonTreeReader.beginObject(JsonTreeReader.java:70)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:189)
... 34 more