Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read json file into java with simple JSON library

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

My JSON file looks like this:

[       {           "name":"John",         "city":"Berlin",         "cars":[               "audi",             "bmw"         ],         "job":"Teacher"     },     {           "name":"Mark",         "city":"Oslo",         "cars":[               "VW",             "Toyata"         ],         "job":"Doctor"     } ] 

This is the java code I wrote to read this file:

package javaapplication1; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.Iterator; import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException;  public class JavaApplication1 {     public static void main(String[] args) {          JSONParser parser = new JSONParser();          try {                  Object obj = parser.parse(new FileReader("c:\\file.json"));              JSONObject jsonObject =  (JSONObject) obj;              String name = (String) jsonObject.get("name");             System.out.println(name);              String city = (String) jsonObject.get("city");             System.out.println(city);              String job = (String) jsonObject.get("job");             System.out.println(job);              // loop array             JSONArray cars = (JSONArray) jsonObject.get("cars");             Iterator<String> iterator = cars.iterator();             while (iterator.hasNext()) {              System.out.println(iterator.next());             }         } catch (FileNotFoundException e) {             e.printStackTrace();         } catch (IOException e) {             e.printStackTrace();         } catch (ParseException e) {             e.printStackTrace();         }     } } 

But I get the following exception:

Exception in thread "main" java.lang.ClassCastException: org.json.simple.JSONArray cannot be cast to org.json.simple.JSONObject at javaapplication1.JavaApplication1.main(JavaApplication1.java:24)

Can somebody tell me what I am doing wrong? The whole file is a array and there are objects and another array (cars) in the whole array of the file. But i dont know how I can parse the whole array into a java array. I hope somebody can help me with a code line which I am missing in my code.

Thanks

like image 604
billz Avatar asked Jun 07 '12 05:06

billz


People also ask

Can Java read JSON?

JSON Processing in Java : The Java API for JSON Processing JSON. simple is a simple Java library that allow parse, generate, transform, and query JSON.


1 Answers

The whole file is an array and there are objects and other arrays (e.g. cars) in the whole array of the file.

As you say, the outermost layer of your JSON blob is an array. Therefore, your parser will return a JSONArray. You can then get JSONObjects from the array ...

  JSONArray a = (JSONArray) parser.parse(new FileReader("c:\\exer4-courses.json"));    for (Object o : a)   {     JSONObject person = (JSONObject) o;      String name = (String) person.get("name");     System.out.println(name);      String city = (String) person.get("city");     System.out.println(city);      String job = (String) person.get("job");     System.out.println(job);      JSONArray cars = (JSONArray) person.get("cars");      for (Object c : cars)     {       System.out.println(c+"");     }   } 

For reference, see "Example 1" on the json-simple decoding example page.

like image 84
Greg Kopff Avatar answered Sep 28 '22 07:09

Greg Kopff