首页 > 解决方案 > 如何解析这个带有内层的 json 文件?

问题描述

JSON:

{  
   "prop":"property",
   "inputInfo":{  
      "sku":"6157068",
      "inputText":"iphone"
   }
}

代码:

JSONObject inputObject = JSON.parseObject(input);
String prop = (String)inputObject.get("property");

但是如何获取'sku'&'inputText'的内层呢?

我在 Java 中使用阿里巴巴 json 库。

标签: javajson

解决方案


我没用过阿里巴巴库,但它没有getJSONObject()你可以使用的方法inputObject吗?我以前做过,但我使用了org.json图书馆。

JSON:

    {"circle":{
        "radius":255819.07998349078,
        "center":{
            "lat":00.000000000000000,
            "lng":00.000000000000000
            }
         }
    }

爪哇

    JSONObject shape = new JSONObject(entity.getJsonLocation());

    double latitude = shape.getJSONObject("circle")
                           .getJSONObject("center")
                           .getDouble("lat");

    double longitude = shape.getJSONObject("circle")
                            .getJSONObject("center")
                            .getDouble("lng");

例如,此示例获取 JSON 并创建一个JSONObject shape. 然后我可以通过调用来获取内部 jsongetJSONObject()对象shape

我希望这可以帮助你。


推荐阅读