0

Hi I have a list of complex JSon Objects. I want to convert it to List of Objects. But I am getting below exceptions.

[
                   {
                    "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 my POJO class

@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;

The EffectiveDate.java

@SerializedName("startDate")
private DateTime startDate;

@SerializedName("endDate")
private DateTime endDate;

I am using below code to convert it to list of Objects

    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);

But I am getting below exceptions:

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
vineeth sivan
  • 510
  • 3
  • 18
Asif Billa
  • 821
  • 2
  • 15
  • 28
  • 2
    Possible duplicate of [GSON throwing "Expected BEGIN\_OBJECT but was BEGIN\_ARRAY"?](http://stackoverflow.com/questions/9598707/gson-throwing-expected-begin-object-but-was-begin-array) – Kruti Patel Feb 15 '16 at 11:29
  • @KrutiPatel it's "but was STRING" here. Can we see a `System.out.println` output for the read json file? (Also you probably should not use hardcoded path like `src/test/` in production code) –  Feb 15 '16 at 12:50

0 Answers0