我的字符串编码的内部JSON文档的Gson解析错误

时间:2017-05-12 05:27:07

标签: java json parsing gson

我尝试使用Gson将服务器中的响应JSON字符串解析为我的POJO对象。以下是我的JSON:

{  
   "user":{  
      "id":"859adb60-4a47-4e1b-8f40-10480bfc33ec",
      "createdAt":"2017-05-11T07:52:43.661Z",
      "updatedAt":"2017-05-11T07:52:43.786Z",
      "version":"AAAAAAA0GXQ=",
      "deleted":false,
      "userName":null,
      "bucketId":"7c2b3ce4-ac5d-4e6a-a6c1-d6316c01fcfb",
      "userEmailId":"test1@tilicho.in",
      "signupType":"Custom",
      "userAvatar":null,
      "userPin":"$2a$10$9TCIqHrE6jDZMr0lhHRddeC7kU.Ob6s8o8zG3ahsmaAMBNi5Gkvwy",
      "verified":true,
      "verifyHash":"IpkzqvjHGb6nbdxxkvmgLiPJRSRhUJ6wMOO3V7Gz5ispOlpLPtdGMTkldzOwHp3Q",
      "thirdPartyToken":null,
      "loginAgent":null,
      "isDataSetUp":false
   },
   "colloboratedChildren":[  

   ],
   "invitations":[  
      {  
         "id":"4892167b-bc6a-47e5-bb30-1785b5643edc",
         "createdAt":"2017-05-11T07:52:06.019Z",
         "updatedAt":"2017-05-11T07:52:06.019Z",
         "version":"AAAAAAA0GWY=",
         "deleted":false,
         "inviterId":"9d00f972-e66d-400b-b332-f0b873a8b1fd",
         "inviterEmail":"nemani@tilicho.in",
         "inviteeId":null,
         "inviteeEmail":"test1@tilicho.in",
         "inviteData":"{\"821e63e7-457e-401f-b071-abacd51bcbad\":[\"B690E26C-BA39-45E3-BB34-CF6EABB666F7\"]}"
      }
   ],
   "authToken":"bQYng6AAeCp9HlR1rCZeOCbFUm1LaRfVzcqTWpgXiLul3tFj7Y4hKr997V6hvl6d"
}

以下是我的POJO课程。

@Getter
@Setter
public class LoginResponse {

    User user;
    ArrayList<ColloboratedChildren> colloboratedChildren;
    ArrayList<InvitationResponse> invitations;
    String authToken;

}

@Getter
@Setter
public class User extends BucketAzureModel {

    private String signupType;
    private String userAvatar;
    private String userEmailId;
    private String userName;
    private String userPin;


}

@Getter
@Setter
ColloboratedChildren {}

public class InvitationResponse {

    String id;
    String createdAt;
    String updatedAt;
    String version;
    boolean deleted;
    String inviterId;
    String inviterEmail;
    String inviteeId;
    String inviteeEmail;
    Map<String, ArrayList<String>> inviteData;

}

将JSON转换为POJO的代码:

JsonEntityParser.parseResults(jsonElement, gsonBuilder.create(), concreteClass);

以下是我面临的错误:

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at path $.invitations[0].inviteData
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:224)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:41)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:82)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:61)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:129)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:220)
at com.google.gson.Gson.fromJson(Gson.java:887)
at com.google.gson.Gson.fromJson(Gson.java:952)
at com.google.gson.Gson.fromJson(Gson.java:925)
at com.microsoft.windowsazure.mobileservices.table.serialization.JsonEntityParser.parseResults(JsonEntityParser.java:64)

从错误中我了解到Gson期待{",但我并不完全了解如何在{{1}创建Map<String, ArrayList<String>>来自给定的响应。我没有真正了解如何定义我的POJO以解析传入的JSON。请注意,我无法更改JSON的格式,我必须遵守此格式。任何帮助都会非常感激。

1 个答案:

答案 0 :(得分:0)

  

com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:   预计BEGIN_OBJECT但是在路径上是STRING   $ .invitations [0] .inviteData

因此,此错误表明您希望inviteData属于Map<String,ArrayList<String>>类型,但返回的内容不是Map String

"inviteData":"{\"821e63e7-457e-401f-b071-abacd51bcbad\":[\"B690E26C-BA39-45E3-BB34-CF6EABB666F7\"]}"
            ||
            ||
            ||
            --->This is a String not of type Map<String,ArrayList<String>>

所以你必须在你的POJO中进行适当的改变,

class InvitationResponse {

    String id;
    String createdAt;
    String updatedAt;
    String version;
    boolean deleted;
    String inviterId;
    String inviterEmail;
    String inviteeId;
    String inviteeEmail;
    String inviteData; ----> This should be String
}

如果您的JSON如下所示:

"inviteData" : {
                "someKey" : [ "SomeValue1","SomeValue2" ]
             }

然后你的解决方案就可以了。

现在因为这个String对你没用,所以要么你必须改变JSON响应,如果你是那个JSON的提供者,否则你需要额外的努力将这个String值再次更改为Map<String,ArrayList<String>>