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

19,187
{
      "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/

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/

Share:
19,187

Related videos on Youtube

Nuno Dores
Author by

Nuno Dores

Updated on September 15, 2022

Comments

  • Nuno Dores
    Nuno Dores over 1 year

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

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

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

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

    • Rahul
      Rahul about 8 years
      you are not using a valid json. Please correct the format
  • Nuno Dores
    Nuno Dores about 8 years
    Unfortunately it gives me the same problem :/
  • Abhishek
    Abhishek about 8 years
    have your checked for the validity of your json file using the link above?
  • Nuno Dores
    Nuno Dores about 8 years
    Exactly , my json is not valid .. my listCar i think
  • Abhishek
    Abhishek about 8 years
    @NunoDores - hope the above is what you wanted... if yes don't forget to mark this answer as the right one... it might help others in the future..
  • Abhishek
    Abhishek about 8 years
    happy to solve your problem... don't forget to upvote and mark this as the correct answer :)