1

I've following JSON Array and I wanted to parse that to get all three email address into a three separate variables. I wanted this to be done using Google GSON library. How we can do that ?

string jsonarrays=

[
       {
          "Id": "0e9b73bc-cf32-48b4-88cc-ee4ff8c3a823",
          "FirstName": "Chris",
          "LastName": "Rogers",
          "Height": "180cm",
          "Weight": "74kg",
          "location": "UK",
          "email": "[email protected]"
       },
       {
          "Id": "0e9b73bc-7cd7-48b4-88cc-ee4ff8c76vcv",
          "FirstName": "Lavina",
          "LastName": "Langer",
          "Height": "170cm",
          "Weight": "63kg",
          "location": "Poland",
          "email": "[email protected]"
       },
       {
          "Id": "213jdsj-cf32-48b4-1119hshjakakaaka",
          "FirstName": "Mike",
          "LastName": "Harford",
          "Height": "184cm",
          "Weight": "80kg",
          "location": "Paris",
          "email": "[email protected]"
       }
    ]

I've write some code

Gson myGson = new Gson();
JsonParser jsonParser = new JsonParser();
JsonArray userArray =jsonParser.parse(jsonarrays).getAsJsonArray();
List messageArray = new ArrayList();
    for (JsonElement aUser : userArray ){
        User aTwitterUser = myGson.fromJson(aUser, User.class);
        messageArray.add(aTwitterUser);
}

The error that I am getting

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.Gson.fromJson(Gson.java:810)
    at com.google.gson.Gson.fromJson(Gson.java:875)
    at com.google.gson.Gson.fromJson(Gson.java:848)
    at com.altruista.mp.rest.messages.MessagesTest.testAPIMessages(MessagesTest.java:113)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    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.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    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)
1
  • Please also share content of User class. Commented May 15, 2015 at 9:45

1 Answer 1

1

Obviously your JSON is wrong as com.google.gson.JsonSyntaxException states. Try removing the last comma from every object. Maybe Gson doesn't like that. Use http://jsonlint.com to validate you JSON.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.