GSON: Expected BEGIN_OBJECT but was STRING

前端 未结 1 662
抹茶落季
抹茶落季 2020-12-11 01:02

I am getting a GSON error trying to unmarshal JSON into an object. The error (Expected BEGIN_OBJECT but was STRING at line 3 column 22) is pointing to line

相关标签:
1条回答
  • 2020-12-11 01:45

    No, you've not mapped it correctly as your json object isn't a BusinessPartnerCreate, it contains a BusinessPartnerCreate.

    You can create a class just to encapsulate your BusinessPartnerCreate but it would be cleaner to deserialize the container as a jsonObject using

     JsonParser parser = new JsonParser();
     JsonObject obj = parser.parse(json).getAsJsonObject();
    

    and then parse the interesting content using

    BusinessPartnerCreate bpc = gson.fromJson(obj.get("business-partner-create"), BusinessPartnerCreate.class);
    

    And I suggest you add an annotation to ensure proper mapping of the partnerType field :

       @SerializedName "partner-type"
       protected JAXBElement<String> partnerType;
    

    (and similar for zip-code)

    0 讨论(0)
提交回复
热议问题