一尘不染

使用gson解析嵌套的JSON

json

{
    "Response": {
        "MetaInfo": {
            "Timestamp": "2011-11-21T14:55:06.556Z"
        },
        "View": [
            {
                "_type": "SearchResultsViewType",
                "ViewId": 0,
                "Result": [
                    {
                        "Relevance": 0.56,
                        "MatchQuality": {
                            "Country": 1,
                            "State": 1,
                            "County": 1,
                            "City": 1,
                            "PostalCode": 1
                        },
                        "Location": {
                            "LocationType": "point",
                            "DisplayPosition": {
                                "Latitude": 50.1105,
                                "Longitude": 8.684
                            },
                            "MapView": {
                                "_type": "GeoBoundingBoxType",
                                "TopLeft": {
                                    "Latitude": 50.1194932,
                                    "Longitude": 8.6699768
                                },
                                "BottomRight": {
                                    "Latitude": 50.1015068,
                                    "Longitude": 8.6980232
                                }
                            },
                            "Address": {
                                "Country": "DEU",
                                "State": "Hessen",
                                "County": "Frankfurt am Main",
                                "City": "Frankfurt am Main",
                                "District": "Frankfurt am Main",
                                "PostalCode": "60311",
                                "AdditionalData": [
                                    {
                                        "value": "Germany",
                                        "key": "CountryName"
                                    }
                                ]
                            }
                        }
                    }
                ]
            }
        ]
    }
}

我正在尝试从上述JSON检索邮政编码。我正在用gson解析它。我是JSON的新手,从我在这里的所有文章中读到的内容(有些与此类似),我都知道字段名称应保持原样。所以我知道我必须做出4类,即响应,视图,结果和地址。我使它们成为静态嵌套类,但是我只得到空值作为输出。在下一个JSON中,我有多个地址。但是我只停留在这个单一的回应上。

举一个简短的例子,我尝试使用此代码检索Timestamp,但它为我提供了一个空值

public class ParseJSON {
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new FileReader("try.json"));

        Gson gson = new GsonBuilder().create();
        Pojo pojo = gson.fromJson(br,Pojo.class);
        System.out.println(Pojo.Response.MetaInfo.Timestamp);
        br.close();
    }
}

class Pojo {
    public Pojo() { }

    static class Response{
        static class MetaInfo {
            static public String Timestamp;

            public String getTimestamp() {
                    return Timestamp;
            }
        }
    }
}

阅读 203

收藏
2020-07-27

共1个答案

一尘不染

如果只需要"PostalCode",则可以使用JsonParser而不是使用一堆类:

JsonParser jsonParser = new JsonParser();
JsonObject address = jsonParser.parse(json)
    .getAsJsonObject().get("Response")
    .getAsJsonObject().getAsJsonArray("View").get(0)
    .getAsJsonObject().getAsJsonArray("Result").get(0)
    .getAsJsonObject().get("Location")
    .getAsJsonObject().getAsJsonObject("Address");
String postalCode = address.get("PostalCode").getAsString();

或所有结果:

JsonArray results = jsonParser.parse(json)
        .getAsJsonObject().get("Response")
        .getAsJsonObject().getAsJsonArray("View").get(0)
        .getAsJsonObject().getAsJsonArray("Result");
for (JsonElement result : results) {
    JsonObject address = result.getAsJsonObject().get("Location").getAsJsonObject().getAsJsonObject("Address");
    String postalCode = address.get("PostalCode").getAsString();
    System.out.println(postalCode);
}
2020-07-27