如何使用 GSON 解析多个嵌套的 JSON objects/arrays?

How to parse multiple nested JSON objects/arrays with GSON?

我一直在努力思考如何使用 GSON 解析嵌套对象和数组,但仍然卡住了。如何解析 "results" 数组中列出的嵌套项?

{
"item": {
    "results": [
        {
            "__metadata": {
                "url": "google.com",
                "type": "website"
            },
            "listed": true,
            "market": 225,
            "town": "Toronto"
        },
        {
            "__metadata": {
                "url": "twitter.com",
                "type": "website"
            },
            "listed": true,
            "market": 225,
            "town": "Calgary"
        }
    ]
}
}

如何在 Android 内使用 GSON 轻松做到这一点?

谢谢!

Android工作室

  1. 下载插件"GsonFormat"
  2. 创建你的模型class
  3. 打开代码->生成->Gson
  4. 粘贴你的 json 点击确定 - 如果 json 有效然后它会将下面的 json 转换为 java class (pojo)
  5. 现在创建 Gson 对象

    Gson gson=new Gson();

  6. 将Json转换为java对象

    T obj = gson.fromJson(内容, tClass);

  7. 现在使用这个对象"obj"获取值