java 如何从文件解析 JSON 数组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39689507/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 04:34:19  来源:igfitidea点击:

How to parse JSON array from file?

javajson

提问by Nikolay Baranenko

I have JSON from file.

我有来自文件的 JSON。

{  
  "from_excel":[  
    {  
      "solution":"Fisrt",
      "num":"1"
    },
    {  
      "solution":"Second",
      "num":"2"
    },
    {  
      "solution":"third",
      "num":"3"
    },
    {  
      "solution":"fourth",
      "num":"4"
    },
    {  
      "solution":"fifth",
      "num":"5"
    }
  ]
}

and want to parse list of Solution and Num.

并想解析解决方案和数量的列表。

Used lib org.json.simple.*

使用 lib org.json.simple.*

Try this one

试试这个

        Object obj = parser.parse(new FileReader("E:\json.txt"));

        JSONObject jsonObject = (JSONObject) obj;

        out.println(jsonObject.get("from_excel"));

        JSONObject obj_new = (JSONObject) jsonObject.get("from_excel");

        JSONArray solution = (JSONArray) obj_new.get("solution");

        Iterator iterator = solution.iterator();
        while (iterator.hasNext()) {
            out.println(iterator.next());
        }

What am I doing wrong?

我究竟做错了什么?

If try to write

如果尝试写

JSONObject solutions = (JSONArray) jsonObject.get("from_excel");

enter image description here

在此处输入图片说明

If try to write

如果尝试写

JSONArray array = obj.getJSONArray("from_excel");

get error enter image description here

得到错误 在此处输入图片说明

回答by Nikolay Baranenko

working solution

工作解决方案

JSONParser parser = new JSONParser();
JSONObject jsonObject;
try {

    jsonObject = (JSONObject) parser.parse(new FileReader("E:\json.txt"));

    out.println("<br>"+jsonObject);


    JSONArray from_excel = (JSONArray)jsonObject.get("from_excel");
    // for row output 1
    for(Object o: from_excel){
        out.println("<br>"+o);
    }
    // for row output 2
    Iterator iterator = from_excel.iterator();
    while (iterator.hasNext()) {
        out.println("<br>"+iterator.next());
    }
    // for item output 3
    for (int i = 0; i < from_excel.size(); i++) {

        JSONObject jsonObjectRow = (JSONObject) from_excel.get(i);
        String num = (String) jsonObjectRow.get("num");
        String solution = (String) jsonObjectRow.get("solution");
        out.println("<br>num="+num+"; solution="+solution);
    }
} catch (Exception e) {
    out.println("Error: "+e);
}

回答by Neeraj

There is no "solution" array in the JSON. "from_excel" is the array. So your code should look like this :

JSON 中没有“解决方案”数组。“from_excel”是数组。所以你的代码应该是这样的:

Object obj = parser.parse(new FileReader("E:\json.txt"));

    JSONObject jsonObject = (JSONObject) obj;

    out.println(jsonObject.get("from_excel"));

    JSONArray solutions = (JSONArray) jsonObject.get("from_excel");

    Iterator iterator = solutions.iterator();
    while (iterator.hasNext()) {
        out.println(iterator.next());
    }

回答by SpringLearner

from_excel is JSON array not an object. So you should achieve it is array.

from_excel 是 JSON 数组而不是对象。所以你应该实现它是数组。

JSONObject jsonObject = (JSONObject) obj;
JSONArray array = obj.getJSONArray("from_excel");

then iterate the array and get each jsonobject. Something like the below

然后迭代数组并获取每个jsonobject。像下面这样的东西

for(int i = 0 ; i < array.length() ; i++){
    array.getJSONObject(i).getString("solution");
}