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/
Related videos on Youtube
Author by
Nuno Dores
Updated on September 15, 2022Comments
-
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 about 8 yearsyou are not using a valid json. Please correct the format
-
-
Nuno Dores about 8 yearsUnfortunately it gives me the same problem :/
-
Abhishek about 8 yearshave your checked for the validity of your json file using the link above?
-
Nuno Dores about 8 yearsExactly , my json is not valid .. my listCar i think
-
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 about 8 yearshappy to solve your problem... don't forget to upvote and mark this as the correct answer :)