{
    "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"
                                    }
                                ]
                            }
                        }
                    }
                ]
            }
        ]
    }
}

I am trying to retrieve the postal code from the above JSON. I am using gson to parse it. I am very new to JSON and from what i read from all the posts here(some very similar to this), I understood that the fields name should be as it is. So I understand i have to make 4 classes viz Response, view, Result and Address. I made them static nested classes, but I am only getting null value as output. In the next JSON, I have multiple addresses. But I am stuck on this single response.

举个简单的例子,我试图用这段代码检索时间戳,但它给了我一个空值

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

推荐答案

如果你只需要"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();

or for all results:

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

Json相关问答推荐

将JSON输入子数组转换为字符串顺序列表输出

如何使用模式注册中心创建从主题中取消本地化的ks qlDB表?

将部分数据字节解组到自定义 struct 中

Golang jsonrpc2 服务器在哪里监听?

在 PowerShell 中通过 aws cli 创建 cloudwatch alert 时出现字符串解析错误

Android 如何判断小时时间是否在两个时间之间?

Spark-SQL中的from_unixtime函数未能给出正确的输出

Rust实现:高效解析任意大小的JSON数组

jq - 仅在键值对存在的地方打印值

使用 JQ 获取 JSON 中的替代元素(输出:JSON 对象)

打印与 JSON 和 PowerShell 中的模式匹配的子项的父项名称

颠簸转换问题

Golang 解组行为:字段过多?

使用 SwiftUI 在 API 调用中解码嵌套 JSON 响应时遇到问题

Go - JSON 验证抛出错误,除非我在 struct 中使用指针.为什么?

在 rust 中从 API 反序列化 serde_json

[__NSCFNumber 长度]:发送到实例 UITableView 的无法识别的 Select 器

在 JSON.NET 中序列化派生类时的字段顺序

ASP.NET Web API JSON 输出中没有时间的日期

将 JSON 模式转换为 python 类