java org.json.simple.JSONArray 不能转换为 org.json.simple.JSONObject

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39161185/
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:07:45  来源:igfitidea点击:

org.json.simple.JSONArray cannot be cast to org.json.simple.JSONObject

javajson

提问by Bufank85

I'm trying to read a JSON file in which I have a series of integer data but when read tells me it can not convert from JSONObject to JSONArray

我正在尝试读取一个 JSON 文件,其中有一系列整数数据,但是当读取时告诉我它无法从 JSONObject 转换为 JSONArray

Part of the JSON file structure is:

JSON 文件结构的一部分是:

{  
    "data": [  
        [1, 1, 1, 1, 1, 1, 1, 1],  
        [1, 1, 0, 0, 0, 0, 1, 1],  
        [1, 1, 0, 0, 0, 0, 1, 1],  
        [0, 1, 1, 0, 0, 1, 1, 0],  
        [0, 1, 1, 1, 1, 1, 1, 0],  
        [0, 0, 1, 1, 1, 1, 0, 0],  
        [0, 0, 0, 0, 0, 0, 0, 0],  
        [0, 0, 0, 0, 0, 0, 0, 0]  
    ],  
    "time": 0.2  
}, 

Code:

代码:

public static void main(String[] args) throws InterruptedException {

JSONParser parser = new JSONParser();


try {

    Object obj = parser.parse(new FileReader("C:\Carriots\dos.json"));

    JSONObject jsonObject = (JSONObject) obj;


    // loop array
    JSONArray tag = (JSONArray) jsonObject.get("data");


    Iterator iterator = tag.iterator();

    while (iterator.hasNext()) {
    System.out.println(iterator.next());

}

回答by ban-geoengineering

Try...

尝试...

JSONArray tag = jsonObject.getJSONArray("data");

JSONArray tag = jsonObject.getJSONArray("data");

回答by Shaishav Jogani

Try to use below to get JSONArray from key data

尝试使用下面从键中获取 JSONArray data

JSONArray tag = (JSONArray) jsonObject.getJSONArray("data");

jsonObject.get()will return you Objectnot any JSONArray.

jsonObject.get()不会给你Object任何回报JSONArray

回答by px06

import java.io.FileReader;

import org.json.simple.JSONObject;
import org.json.simple.JSONArray;
import org.json.simple.parser.JSONParser;

public class JSONTest {

    public static void main(String[] args){


        try {

            JSONParser parser = new JSONParser();

            Object obj = parser.parse(new FileReader("C:\Carriots\dos.json"));

            JSONObject jsonObject = (JSONObject) obj;

            JSONArray data = (JSONArray)jsonObject.get("data");

            for(Object o: data){
                System.out.println(o);
            }

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }


}

output:

输出:

[1,1,1,1,1,1,1,1]
[1,1,0,0,0,0,1,1]
[1,1,0,0,0,0,1,1]
[0,1,1,0,0,1,1,0]
[0,1,1,1,1,1,1,0]
[0,0,1,1,1,1,0,0]
[0,0,0,0,0,0,0,0]
[0,0,0,0,0,0,0,0]

回答by lars

You can use an iterator to process the array data. Be sure to use Object rather than String or Integer since you will get errors converting JSONObject to one of these values.

您可以使用迭代器来处理数组数据。请务必使用 Object 而不是 String 或 Integer,因为将 JSONObject 转换为这些值之一时会出错。

package jsonProcessing;

import java.io.FileReader;
import java.util.Iterator;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;

public class JsonNumReader {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    JSONParser parser = new JSONParser();

    try {
        JSONObject jsonObject = (JSONObject) parser.parse(new FileReader("C:/JSON/numbers.json"));

        JSONArray array = (JSONArray)jsonObject.get("data");

        Iterator<Object>iterator = array.iterator();
        while(iterator.hasNext()){
            System.out.println(iterator.next());
        }   
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
  }
}

Output:

输出:

[1,1,1,1,1,1,1,1]
[1,1,0,0,0,0,1,1]
[1,1,0,0,0,0,1,1]
[0,1,1,0,0,1,1,0]
[0,1,1,1,1,1,1,0]
[0,0,1,1,1,1,0,0]
[0,0,0,0,0,0,0,0]
[0,0,0,0,0,0,0,0]