如何从 json 文件中逐一获取多个 json 数据?

How can I get multiple json data from json file one by one?

我正在尝试从 json 文件中获取特定值。这是我的 json 文件。

{
  "books": ["book1","book2","book3","book4"],
  "set_with_ratio": {
    "tweaks_es": "7",
    "ratio": {
      "brand_defined_sets_ratio": {
        "default": {
          "desktop": {
            "16": "85",
            "11": "95",
            "19": "10",
            "12": {
              "2": "50"
            }
          }
        },
        "rentals": {
          "desktop": {
            "16": "75",
            "11": "45",
            "12": {
              "2": "10"
            }
          }
        }
      }
    }
  }
}

怎样才能一一获取books个数组元素?我试过了。

JSONParser jsonParser = new JSONParser();
JSONObject jsonObject = (JSONObject) jsonParser.parse(jsonData);
JSONArray jsonArray = (JSONArray) jsonObject.get("books");
//Iterating the images of the array
Iterator<Object> iterator = jsonArray.iterator();
while(iterator.hasNext()) {
 System.out.println(iterator.next());
 }

由此我得到了这个错误java.lang.ClassCastException: org.json.simple.JSONArray cannot be cast to org.json.JSONArray 如何获取 x.set_with_ratio.ratio.brand_defined_sets_ratio.default.desktop.11 此 json 路径的数据。我正在 Java 中尝试此代码。

JSONParser jsonParser = new JSONParser();
        try {
            //Parsing the contents of the JSON file
            JSONObject jsonObject = (JSONObject) jsonParser.parse(new FileReader("sample.json"));
            String id = jsonObject.get("set_with_ratio").toString();
            System.out.println(id);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        }

这里我得到了全部 json 文件,但无法获得预期的结果。

试试这个:

JSONTokener parser = new JSONTokener(new FileReader("sample.json"));
JSONObject jsonObject = new JSONObject(parser);
String id = jsonObject.getJSONObject("set_with_ratio").getJSONObject("ratio").getJSONObject("brand_defined_sets_ratio").getJSONObject("default").getJSONObject("desktop").getString("11");

使用的库:org.json

试试这个

import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.junit.Test;


public class JavaTest {
    @Test
    public void name() {
        JSONParser jsonParser = new JSONParser();
        try {
            //Parsing the contents of the JSON file
            JSONObject jsonObject = (JSONObject) jsonParser.parse("{\n" +
                    "  \"set_with_ratio\": {\n" +
                    "    \"tweaks_es\": \"7\",\n" +
                    "    \"ratio\": {\n" +
                    "      \"brand_defined_sets_ratio\": {\n" +
                    "        \"default\": {\n" +
                    "          \"desktop\": {\n" +
                    "            \"16\": \"85\",\n" +
                    "            \"11\": \"95\",\n" +
                    "            \"19\": \"10\",\n" +
                    "            \"12\": {\n" +
                    "              \"2\": \"50\"\n" +
                    "            }\n" +
                    "          }\n" +
                    "        },\n" +
                    "        \"rentals\": {\n" +
                    "          \"desktop\": {\n" +
                    "            \"16\": \"75\",\n" +
                    "            \"11\": \"45\",\n" +
                    "            \"12\": {\n" +
                    "              \"2\": \"10\"\n" +
                    "            }\n" +
                    "          }\n" +
                    "        }\n" +
                    "      }\n" +
                    "    }\n" +
                    "  }\n" +
                    "}");
            String id = ((JSONObject) ((JSONObject) ((JSONObject) ((JSONObject) ((JSONObject) jsonObject.get("set_with_ratio"))
                    .get("ratio"))
                    .get("brand_defined_sets_ratio"))
                    .get("default"))
                    .get("desktop"))
                    .get("11" +
                    "").toString();
            System.out.println(id);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}

结果是95