Gson deserialize json of embedded member

限于喜欢 提交于 2019-12-12 04:15:15

问题


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

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.

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

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?


回答1:


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

Run below code:

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

Output:

famousId:23|45

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

Output:

famousId:23|45


来源:https://stackoverflow.com/questions/25768137/gson-deserialize-json-of-embedded-member

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!