gpt4 book ai didi

java - GSON : Expected BEGIN_OBJECT but was STRING

转载 作者:太空狗 更新时间:2023-10-29 22:54:33 26 4
gpt4 key购买 nike

我在尝试将 JSON 解码为对象时遇到 GSON 错误。错误(Expected BEGIN_OBJECT but was STRING at line 3 column 22)指向下方输入的第 3 行。

我是否没有根据 Bean 正确映射 JSON?

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

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

{
"business-partner-create": {
"partner-type": "1",
"person": {
"firstName": "Dirk",
"lastName": "Wintermill",
"title": ""
},
"email": "kirk@yahoo.com",
"phone": "219-385-2946",
"addressClean": {
"house-number": "10218",
"street-name": "Park",
"street-abbr": "Rd"
},
"city": "Somerset",
"state": "NJ",
"zip-code": "01955"
}
}

最佳答案

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

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

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

然后使用

解析有趣的内容
BusinessPartnerCreate bpc = gson.fromJson(obj.get("business-partner-create"), BusinessPartnerCreate.class);

我建议您添加注释以确保正确映射 partnerType 字段:

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

(和邮政编码类似)

关于java - GSON : Expected BEGIN_OBJECT but was STRING,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11214720/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com