如何从json文件一个接一个地获取多个json数据? [英] How can I get multiple json data from json file one by one?

查看:466
本文介绍了如何从json文件一个接一个地获取多个json数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to get specific value from json file. Here is my json file.

{
  "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数组元素?我尝试过了.

How can I get books array elements one by one? I tried this.

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尝试这段代码.

From this I got this error java.lang.ClassCastException: org.json.simple.JSONArray cannot be cast to org.json.JSONArray how can I get data for x.set_with_ratio.ratio.brand_defined_sets_ratio.default.desktop.11 this json path. I am trying this code in 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文件总数,但无法获得预期的结果.

Here I get the total json file but cannot manage to get expected result.

推荐答案

尝试一下

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

这篇关于如何从json文件一个接一个地获取多个json数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆