org.json.JSONException:找不到JSONObject ["ListeCar"] [英] org.json.JSONException: JSONObject["ListeCar"] not found

查看:137
本文介绍了org.json.JSONException:找不到JSONObject ["ListeCar"]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用JSON库使用Java读取此JSON文件

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

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

这是我的Java代码:

This is my java code :

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

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

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 ...

推荐答案

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

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

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/

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

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

您忘了解析json ...,在上面的代码中完成了....有关如何执行此操作的教程的链接如下:: http://crunchify.com/如何从Java文件中读取json对象对象/

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/

这篇关于org.json.JSONException:找不到JSONObject ["ListeCar"]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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