57793

How to access JSONArray in a JSONObject using Java?

Question:

I am having a problem in accessing the JSONArray ("pages") while reading a json file in Java using the json-simple-1.1.1.jar

My json file is around 32MB of size and the format is as below:

{ "id": "0912898213", "pages": [ { "pageUrl": "http://www.example0.com", "results": [ { "number": "1" }, { "number": "2" } ] }, { "pageUrl": "http://www.example1.com", "results": [ { "number": "3" }, { "number": "4" } ] } ] }

Whereas, the Java code to access this json file is as below:

JSONParser parser=new JSONParser(); JSONObject pagesObject = (JSONObject) parser.parse(new FileReader(PATH_JSON_DataExtractor)); JSONArray jsonArray= (JSONArray) pagesObject.get("pages"); for(int i=0; i<jsonArray.size(); i++){}

<strong>PROBLEM:</strong> The jsonArray is null all the time. Although, the json format is correct and it should work as expected! The above Java code works with the given sample json (also above) but the Java code doesn't work with the 32MB json file. The location of the json file is also correct and the format is also correct, but still I am getting this access issue!

Where I am going wrong to access the json file? I have been looking around on similar questions, and I have followed the exact instructions to access the json file. But I am just lost in getting it right, therefore, looking for suggestions to make this code work. Thank you very much for your time!

Answer1:

With the below code it is working perfect for me. Can you check whether the specified file location is correct? Also try reading id like pagesObject.get("id")

package json.simple; import java.io.FileReader; import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; public class ReadJSON { public static void main(String[] args) throws Exception { JSONParser parser = new JSONParser(); JSONObject pagesObject = (JSONObject) parser.parse(new FileReader("/home/user/tmp/test.json")); System.out.println(pagesObject.get("id")); System.out.println(pagesObject.get("pages").getClass().getName()); JSONArray jsonArray= (JSONArray) pagesObject.get("pages"); for(int i=0; i<jsonArray.size(); i++){ System.out.println(jsonArray.get(i)); } } }

And here is the content of my test.json. Exactly same as yours

{ "id": "0912898213", "pages": [ { "pageUrl": "http://www.example0.com", "results": [ { "number": "1" }, { "number": "2" } ] }, { "pageUrl": "http://www.example1.com", "results": [ { "number": "3" }, { "number": "4" } ] } ] }

And here is my dependency in maven

<dependency> <groupId>com.googlecode.json-simple</groupId> <artifactId>json-simple</artifactId> <version>1.1.1</version> </dependency>

Answer2:

Finally its working now, but I still do not know the actual cause of the problem. I partitioned the file into two separate json files, and executed the code on both of them and it worked. Now I just have to merge them and its done! Not a good solution but couldn't find another way!

Answer3:

Use this:-

<blockquote>

JSONArray jsonArray= (JSONArray) pagesObject.getJSONArray("pages");

</blockquote>

Recommend

  • Cordova 2.7.0 PushPlugin “cordova cannot be resolved” and “ctx cannot be resolved”
  • Showing Error :Attempt to invoke interface method 'int java.util.List.size()' on a null ob
  • Set Facebook graph Request to always return English results
  • Android: convert Parcelable to JSON
  • Remove a key-value pair from javax.json.JsonObject
  • Set Specific Color to the Region Occupied by LineChart
  • Java Parser Json as array multidimensional
  • JSON Array to ImageView in GridView
  • How can we extract the main verb from a sentence?
  • Get max ID and min ID from specific column in MySQL php
  • ANTLR4: Unrecognized constant value in a lexer command
  • Python: how to split and return a list from a function to avoid memory error
  • Android Login- checking if username and password match with MySQL database
  • How to parse Java properties which contains variables?
  • Treetop basic parsing and regular expression usage
  • Regex for Specific Tag
  • Is it possible to run clang with llc flags
  • Adding elements to a huge XML file
  • How integrated is Collada to OpenGL ES
  • Jackson Parser: ignore deserializing for type mismatch
  • How to make a tree having multiple type of nodes and each node can have multiple child nodes in java
  • Running a C# exe file
  • Join two tables and save into third-sql
  • How to model a transition system with SPIN
  • ORA-29908: missing primary invocation for ancillary operator
  • Rearranging Cells in UITableView Bug & Saving Changes
  • Linker errors when using intrinsic function via function pointer
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • How to get Windows thread pool to call class member function?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • LevelDB C iterator
  • Sorting a 2D array using the second column C++
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How can i traverse a binary tree from right to left in java?
  • UserPrincipal.Current returns apppool on IIS
  • Converting MP3 duration time
  • java string with new operator and a literal