大家好我写的反序列化代码如下所示
public class JsonDataDeserialisation implements JsonDeserializer<JsonData>{
@Override
public JsonData deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
// TODO Auto-generated method stub
JsonObject jobj = json.getAsJsonObject();
String jsontype = jobj.get("type").getAsString();
String jsonname = jobj.get("name").getAsString();
String jsondoc = jobj.get("doc").getAsString();
Fields[] fields = context.deserialize(jobj.get("fields"), Fields[].class);
JsonData jdata = new JsonData();
jdata.setType(jsontype);
jdata.setName(jsonname);
jdata.setDoc(jsondoc);
jdata.setFields(fields);
return jdata;
}
}
给出了json事件。
{
"type": "record",
"name": "Doc",
"doc": "adoc",
fields: [{
"name": "id",
"type": "string"
}]
}
但是当涉及像
这样的字段数组中的多个对象时 {
"type": "record",
"name": "Doc",
"doc": "adoc",
fields: [{
"name": "id",
"type": "string"
}, {
"name": "id1",
"type": "string1"
},: {
"name": "id2",
"type": "string2"
}]
}
我尝试了这段代码并向我展示了
线程中的异常&#34; main&#34; java.lang.UnsupportedOperationException:JsonObject错误
public class JsonDataDeserialisation implements JsonDeserializer<JsonData>{
@Override
public JsonData deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
// TODO Auto-generated method stub
JsonObject jobj = json.getAsJsonObject();
String jsontype = jobj.get("type").getAsString();
String jsonname = jobj.get("name").getAsString();
String jsondoc = jobj.get("doc").getAsString();
Fields[] fields = context.deserialize(jobj.get("fields"), Fields[].class);
JsonArray fields1 =jobj.get("fields").getAsJsonArray();
String[] fieldst = new String[fields1.size()];
for(int i=0;i<fieldst.length;i++){
JsonElement jsonfields = fields1.get(i);
fieldst[i] = jsonfields.getAsString();
}
JsonData jdata = new JsonData();
jdata.setType(jsontype);
jdata.setName(jsonname);
jdata.setDoc(jsondoc);
jdata.setFields(fields);
return jdata;
}
}
我哪里出错了请帮助我谢谢你