【发布时间】:2019-10-02 09:45:13
【问题描述】:
我有一个使用 AVRO 自动生成的 JSON 模式类。我想使用这个 JSON 创建一个 GSON 对象。我尝试使用此代码这样做
@Test
public void parseJson() {
JSONParser jsonParser = new JSONParser();
try {
JSONObject jsonObject = (JSONObject) jsonParser.parse(new FileReader("/pathto/test.json"));
ThinEvent thinEvent = new Gson().fromJson(jsonObject.toString(), ThinEvent.class);
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
}
但这会导致以下错误...看起来 GSON 正在寻找一个字符串,但 json 实际上包含一个嵌套对象? ThinEvent 对象已将 refService 声明为列表。
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected a string but was BEGIN_OBJECT at line 1 column 74 path $.references[0].refService
private java.util.List<com.lm.gde.eventing.avro.Reference> references;
这是我的 JSON,我用 XXXX 替换了一些值。
{
"eventType": "policy.PolicyPremiumChangedEvent",
"correlationId": "XXXX",
"references": [
{
"ref": "XXX",
"refType": "policy_id",
"refService": {
"com.lm.gde.eventing.avro.RefService": "policy_service"
},
"links": {
"array": [
{
"refUri": ""
}
]
}
},
{
"ref": "XXXXXX",
"refType": "policy_number",
"refService": {
"com.lm.gde.eventing.avro.RefService": "policy_service"
},
"links": {
"array": [
{
"refUri": "XXXXXX"
}
]
}
},
{
"ref": "2019-09-28",
"refType": "policy_tx_effective_date",
"refService": {
"com.lm.gde.eventing.avro.RefService": "policy_service"
},
"links": {
"array": [
{
"refUri": "XXXXX"
}
]
}
}
],
"eventContext": null,
"Timestamp": 1569574003295,
"Version": "1"
}
【问题讨论】:
标签: java json parsing gson confluent-schema-registry