且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从json文件一个接一个地获取多个json数据?

更新时间:2023-02-01 21:45:07

尝试一下

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