13848

How to read json file (array form) using simple JSON library?

Question:

Actually, The example below is an answer somewhere in StackOverFlow. I tried to use the below code, but,

JSONArray a = (JSONArray) parser.parse(new FileReader("c:\\exer4-courses.json"));

Above line doesn't work because of the below Exception.

java.lang.ClassCastException: org.json.simple.JSONObject cannot be cast to org.json.simple.JSONArray

Is there any way to read my JSON file?

JSON file:

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

Java code:

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) jsonObject.get("cars"); for (Object c : cars) { System.out.println(c + ""); } }

Answer1:

The exception is quite clear. You need to cast to JSONObject and not JSONArray

JSONObject a = (JSONObject) parser.parse(new FileReader("c:\\exer4-courses.json"));

You could possibly have this structure for your JSON:

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

Now to iterate you can do:

JSONArray records = (JSONArray)a.get("records"); for (Object o : records) { 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) jsonObject.get("cars"); for (Object c : cars) { System.out.println(c + ""); } }

Recommend

  • java.lang.ClassCastException: javax.faces.component.StateHolderSaver cannot be cast to [Ljava.lang.O
  • Remote interface cannot be cast to EJB container
  • Bypassing grails view resolvers?
  • Set Specific Color to the Region Occupied by LineChart
  • Java Parser Json as array multidimensional
  • Casting JSONArray to Iterable - Kotlin
  • Get max ID and min ID from specific column in MySQL php
  • Android Login- checking if username and password match with MySQL database
  • How to retrieve a specific string from a specific list from a file with JSON in Python
  • RavenDB indexing errors
  • Haskell: function composition with anonymous/lambda function
  • Multiple Left Join LINQ-to-entities
  • Creating a Multidimensional, Associative Array in VBScript
  • Suqueries in select clause with JPA
  • Default parameter as generic type
  • Unable to install Git-core+svn by MacPorts
  • Django simple Captcha “No module named fields” error
  • Problem deserializing objects from cache on MyBatis 3/Java
  • Play WS (2.2.1): post/put large request
  • Could not find rake using whenever rails
  • OpenGL ES texture problem, 4 duplicate columns and horizontal lines (Android)
  • ImageMagick, replace semi-transparent white with opaque white
  • Highlight one bar in a series in highcharts?
  • Cannot connect to cassandra from Spark
  • Display issues when we change from one jquery mobile page to another in firefox
  • Different response to non-authenticated users and AJAX calls
  • Cross-Platform Protobuf Serialization
  • Alternatives to the OPTIONAL fallback SPARQL pattern?
  • Why winpcap requires both .lib and .dll to run?
  • Python - Map / Reduce - How do I read JSON specific field in using DISCO count words example
  • Warning: Can't call setState (or forceUpdate) on an unmounted component
  • Hibernate gives error error as “Access to DialectResolutionInfo cannot be null when 'hibernate.
  • Run Powershell script from inside other Powershell script with dynamic redirection to file
  • How to format a variable of double type
  • Load html files in TinyMce
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • coudnt use logback because of log4j
  • Can Visual Studio XAML designer handle font family names with spaces as a resource?
  • Is it possible to post an object from jquery to bottle.py?
  • JaxB to read class hierarchy