
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");