简体   繁体   English

如何从文件解析JSON数组?

[英]How to parse JSON array from file?

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");

在此处输入图片说明

If try to write 如果尝试写

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

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

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);
}

There is no "solution" array in the JSON. JSON中没有“解决方案”数组。 "from_excel" is the array. “ from_excel”是数组。 So your code should look like this : 因此,您的代码应如下所示:

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());
    }

from_excel is JSON array not an object. from_excel是JSON数组,不是对象。 So you should achieve it is array. 因此,您应该实现它是数组。

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

then iterate the array and get each jsonobject. 然后迭代数组并获取每个jsonobject。 Something like the below 像下面这样

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM