本文介绍了GSON:预计BEGIN_OBJECT但是STRING的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试将JSON解组为对象时遇到GSON错误。错误(预期BEGIN_OBJECT但在第3行第22列STRING )指向下面输入的第3行。



我是否未映射关于Bean的JSON是否正确?

  import javax.xml.bind.JAXBElement; 

公共类BusinessPartnerCreate {
protected JAXBElement< String> partnerType;
受保护的人;
保护公司公司;
protected String email;
protected String phone;
protected AddressData addressData;
protected AddressClean addressClean;
protected String city;
protected String状态;
protected String zipCode;
protected JAXBElement< String>外部ID;
}

我输入的JSON看起来是这样的:

  {
business-partner-create:{
partner-type:1,
person :{
firstName:Dirk,
lastName:Wintermill,
title:
},
电子邮件 :kirk @ yahoo.com,
电话:219-385-2946,
addressClean:{
house-number:10218,
street-name:Park,
street-abbr:Rd
},
city:Somerset,
state :NJ,
邮政编码:01955
}
}


解决方案

不,你没有正确映射,因为你的json对象不是 BusinessPartnerCreate ,它包含 BusinessPartnerCreate



您可以创建一个类来封装您的 BusinessPartnerCreate 但是使用

 <$将容器反序列化为 jsonObject 会更清晰c $ c> JsonParser解析器= new JsonParser(); 
JsonObject obj = parser.parse(json).getAsJsonObject();

然后使用



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

我建议你添加一个注释,以确保正确映射 partnerType 字段:

  @SerializedNamepartner-type
protected JAXBElement< String> partnerType;

(和邮政编码类似)


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 3 of the input below.

Have I not mapped the JSON correctly with respect to the Bean?

import javax.xml.bind.JAXBElement;

public class BusinessPartnerCreate {
    protected JAXBElement<String> partnerType;
    protected Person person;
    protected Company company;
    protected String email;
    protected String phone;
    protected AddressData addressData;
    protected AddressClean addressClean;
    protected String city;
    protected String state;
    protected String zipCode;
    protected JAXBElement<String> externalId;
}

And my input JSON looks is this:

{
    "business-partner-create": {
        "partner-type": "1",
        "person": {
            "firstName": "Dirk",
            "lastName": "Wintermill",
            "title": ""
        },
        "email": "[email protected]",
        "phone": "219-385-2946",
        "addressClean": {
            "house-number": "10218",
            "street-name": "Park",
            "street-abbr": "Rd"
        },
        "city": "Somerset",
        "state": "NJ",
        "zip-code": "01955"
    }
}
解决方案

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)

这篇关于GSON:预计BEGIN_OBJECT但是STRING的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 11:46