如何使用Google gson解析json数组

时间:2015-05-15 09:43:13

标签: json gson

我已经关注了JSON数组,我想解析它以将所有三个电子邮件地址分成三个独立的变量。 我希望使用Google GSON库完成此操作。我们怎么做到这一点?

string jsonarrays =

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

我已经写了一些代码

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

我得到的错误

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 个答案:

答案 0 :(得分:1)

显然,您的JSON错误,因为com.google.gson.JsonSyntaxException指出。尝试从每个对象中删除最后一个逗号。也许Gson不喜欢那样。 使用http://jsonlint.com验证您的JSON。