从嵌套的 JSON 对象中检索值

2022-09-03 03:32:44

我有JSON文件,我想解析它。JSON 文件(“myfile”)的格式如下:

{
    "LanguageLevels": {
        "1": "Początkujący",
        "2": "ŚrednioZaawansowany",
        "3": "Zaawansowany",
        "4": "Ekspert"
    }
}

我想从语言级别检索密钥 2 的值 (ŚrednioZaawansowany)。

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

public class JsonSimpleExample {
public static void main(String[] args) {

JSONParser parser = new JSONParser();

try {

    Object obj = parser.parse(new FileReader("myfile"); 
    JSONObject jsonObject = (JSONObject) obj;
    JSONObject jsonChildObject = (JSONObject)jsonObject.get("LanguageLevels");

下一步做什么?如何迭代它?


答案 1

也许你没有使用Java库的最新版本的JSON。

json-simple很长一段时间没有更新,而更新的是2个月前。JSON-Java

JSON-Java可以在GitHub上找到,这里是其存储库的链接:https://github.com/douglascrockford/JSON-java

切换库后,您可以参考下面的示例代码:

public static void main(String[] args) {
    String JSON = "{\"LanguageLevels\":{\"1\":\"Pocz\\u0105tkuj\\u0105cy\",\"2\":\"\\u015arednioZaawansowany\",\"3\":\"Zaawansowany\",\"4\":\"Ekspert\"}}\n";

    JSONObject jsonObject = new JSONObject(JSON);
    JSONObject getSth = jsonObject.getJSONObject("LanguageLevels");
    Object level = getSth.get("2");

    System.out.println(level);
}

作为开源的,您可以阅读代码及其文档,它们将指导您完成整个过程。JSON-Java

希望它有所帮助。


答案 2

您必须逐步迭代到嵌套的 JSON 中。

例如,从Google地理编码api接收的JSON

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "Bhopal",
               "short_name" : "Bhopal",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Bhopal",
               "short_name" : "Bhopal",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Madhya Pradesh",
               "short_name" : "MP",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "India",
               "short_name" : "IN",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Bhopal, Madhya Pradesh, India",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 23.3326697,
                  "lng" : 77.5748062
               },
               "southwest" : {
                  "lat" : 23.0661497,
                  "lng" : 77.2369767
               }
            },
            "location" : {
               "lat" : 23.2599333,
               "lng" : 77.412615
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 23.3326697,
                  "lng" : 77.5748062
               },
               "southwest" : {
                  "lat" : 23.0661497,
                  "lng" : 77.2369767
               }
            }
         },
         "place_id" : "ChIJvY_Wj49CfDkR-NRy1RZXFQI",
         "types" : [ "locality", "political" ]
      }
   ],
   "status" : "OK"
}

我将在下面以给定的方式迭代“位置”:{ “lat”:23.2599333,“lng”:77.412615

recieve JSON in json object

        JSONObject json = new JSONObject(output.toString());
        JSONArray result = json.getJSONArray("results");
        JSONObject result1 = result.getJSONObject(0);
        JSONObject geometry = result1.getJSONObject("geometry");
        JSONObject locat = geometry.getJSONObject("location");

        //"iterate onto level of location";

        double lat = locat.getDouble("lat");
        double lng = locat.getDouble("lng");