java org.json.JSONException: JSONObject["ListeCar"] 未找到

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

org.json.JSONException: JSONObject["ListeCar"] not found

javajsonnetbeans

提问by Nuno Dores

I want to read this JSON file with java using json library

我想使用 json 库用 java 读取这个 JSON 文件

"ListeCar": [
    {
        "id": "R",
        "size": "2",
        "Orientation": "Horizontal",
        "Position": {
            "Row": "2",
            "Column": "0"
        }
    }

This is my java code :

这是我的java代码:

package rushhour;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;
import org.json.*;


public class JsonClass {
public static void main(String[] args) throws IOException, JSONException {


    try{
        JSONObject obj = new JSONObject(new FileReader("C:\Users\Nuno\Desktop\School\clg-g41326\RushHourJson.json"));

        JSONObject jsonObject =  (JSONObject) obj;

        JSONArray Liste = obj.getJSONArray("ListeCar");
        String listeCar = Liste.getJSONObject(0).getString("id");
        for (int i = 0; i <Liste.length(); i++) {
        String id = Liste.getJSONObject(i).getString("id");
        System.out.println(id);
        String size = Liste.getJSONObject(i).getString("size");
        System.out.println(size);
        String Orientation = Liste.getJSONObject(i).getString("Orientation");
        System.out.println(Orientation);
        String Position = Liste.getJSONObject(i).getString("Position");
        System.out.println(Position);    
        }
    }catch(JSONException e){
                e.printStackTrace();
                }
}
}

I'm doing this in netbeans and it's kind a my first time using Json ! I want just to do a system.out from this little json code. I don't know why he's not finding the file that i put in the new JSONObjet ...

我在 netbeans 中这样做,这是我第一次使用 Json !我只想从这个小小的 json 代码中做一个 system.out 。我不知道他为什么找不到我放在新 JSONObjet 中的文件...

回答by Abhishek

{
      "ListeCar":[
       {
           "id":"R",
           "size":"2",
           "Orientation":"Horizontal",
           "Position":{
                "Row":"2",
                "Column":"0"
            }
      }]
}

try placing this in your .json file your json is not valid... try placing it in this site to check for it's validity.... http://json.parser.online.fr/

尝试将其放入您的 .json 文件中,您的 json 无效...尝试将其放入此站点以检查其有效性.... http://json.parser.online.fr/

And the code for the correct output....

以及正确输出的代码......

public static void main(String[] args) throws IOException, JSONException, ParseException {
    try {
        JSONParser parser = new JSONParser();
        Object obj = parser.parse(new FileReader("/home/Desktop/temp.json"));
        JSONObject objJsonObject = new JSONObject(obj.toString());
        System.out.println(objJsonObject);
        JSONArray Liste = objJsonObject.getJSONArray("ListeCar");
        String listeCar = Liste.getJSONObject(0).getString("id");
        for (int i = 0; i < Liste.length(); i++) {
            String id = Liste.getJSONObject(i).getString("id");
            System.out.println(id);
            String size = Liste.getJSONObject(i).getString("size");
            System.out.println(size);
            String Orientation = Liste.getJSONObject(i).getString("Orientation");
            System.out.println(Orientation);
            String Position = Liste.getJSONObject(i).getJSONObject("Position").toString();
            System.out.println(Position);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

You forgot to parse json... which is done in the above code.... a link about the tutorial on how to do this is as follows:: http://crunchify.com/how-to-read-json-object-from-file-in-java/

你忘了解析 json ......这是在上面的代码中完成的......关于如何做到这一点的教程的链接如下:http: //crunchify.com/how-to-read-json- java中的文件中的对象/