Gson反序列化嵌入式成员的json [英] Gson deserialize json of embedded member
问题描述
我有以下示例JSON:
I have the following sample JSON:
{
"birds":[
{
"id":"SAMPLEID",
"isTest":true,
"externalId":{
"Main":[
"123ABC"
],
"Sub":[
"456"
]
},
"dinos":[
],
"rhinos":[
{
"id":"SUPER100ID",
"isTest":true,
"externalId":{
"famousId":[
"23|45"
]
},
"dinos":[
],
"pelicans":[
{
"id":"D4CLIK",
"isTest":true,
"bird":null,
"crazyArray":[
]
},
{
"id":"DID123",
"type":"B",
"name":"TONIE",
"isTest":true,
"bird":null,
"subspecies":[
]
}
]
}
]
}
],
"metaData":{
"count":1
}
}
我想使用GSON反序列化这个JSON字符串并仅获得famousId成员的值。
I want to use GSON to deserialize this JSON String and get the value of "famousId" member only.
我看过其他答案,看来我绝对需要为此创建类。
I have looked through other answers and it seems that I will absolutely need to create classes for this.
是否有可能反序列化这个没有映射POJO,使用JsonParser,JsonElement,JsonArray等?我已经尝试了几种排列组合,但没有成功。
Would it be possible to deserialize this without mapping POJOs, using JsonParser, JsonElement, JsonArray, etc? I have tried several permutations of this but with no success.
我也尝试了下面的代码,但它也不能按预期工作:
I also have tried the following code but it is also not working as expected:
JsonObject o = new JsonParser().parse(jsonResponseString).getAsJsonObject();
Gson gson = new Gson();
enterprises ent = new enterprises();
ent = gson.fromJson(o, enterprises.class);
@Getter
@Setter
class birds {
@JsonProperty("rhinos")
List<Rhino> rhinos = new ArrayList<Rhino>();
}
@Getter
@Setter
class Rhino {
@JsonProperty("externalId")
ExternalId externalId;
}
@Getter
@Setter
@JsonPropertyOrder({
"famousId"
})
class ExternalId {
@JsonProperty("famousId")
List<String> famousId = new ArrayList<String>();
}
不幸的是,这也不起作用,所以我猜两个部分的问题。 .is有可能简单地反序列化并得到我想要的famousId的字符串值,以及我当前的类结构有什么不正确的地方?
Unfortunately this does not work either, so I guess a two part question...is it possible to simply deserialize and get the String value for famousId that I want, and what is incorrect with my current class structure?
推荐答案
你几乎完成了。我为您的json结构添加了一个根类( Enterprises
)。
You've almost done. I added a root class(Enterprises
) for your json structure.
class Enterprises {
List<Birds> birds;
}
class Birds {
List<Rhino> rhinos;
}
class Rhino {
ExternalId externalId;
}
class ExternalId {
List<String> famousId;
}
运行下面的代码:
JsonObject o = new JsonParser().parse(jsonResponseString).getAsJsonObject();
Gson gson = new Gson();
Enterprises enterprises = gson.fromJson(o, Enterprises.class);
System.out.println("famousId:" + enterprises.birds.get(0).rhinos.get(0).externalId.famousId.get(0));
输出:
famousId:23|45
或者如果您不想使用pojo类:
Or if you don't want to use pojo classes:
JsonObject o = new JsonParser().parse(jsonResponseString).getAsJsonObject();
JsonArray birdsJsonArray = (JsonArray) o.get("birds");
JsonArray rhinosJsonArray = (JsonArray)((JsonObject)(birdsJsonArray.get(0))).get("rhinos");
JsonObject externalIdJsonObject = (JsonObject)((JsonObject)(rhinosJsonArray.get(0))).get("externalId");
JsonArray famousIdJsonArray = (JsonArray)externalIdJsonObject.get("famousId");
System.out.println("famousId:" + famousIdJsonArray.get(0));
输出:
famousId:23|45
这篇关于Gson反序列化嵌入式成员的json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!