Download the ZIP file from this URL and extract it to get the Jar. Add the Jar to your build path. To check the available classes in this Jar use this URL.
To Add this Jar to your build path Right click the Project > Build Path > Configure build path> Select Libraries tab > Click Add External Libraries > Select the Jar file Download
I hope this will solve your problem
Answer from Ramkumar Murugadoss on Stack OverflowVideos
Download the ZIP file from this URL and extract it to get the Jar. Add the Jar to your build path. To check the available classes in this Jar use this URL.
To Add this Jar to your build path Right click the Project > Build Path > Configure build path> Select Libraries tab > Click Add External Libraries > Select the Jar file Download
I hope this will solve your problem
You should take the json implementation from here : http://code.google.com/p/json-simple/ .
- Download the *.jar
- Add it to the classpath (right-click on the project, Properties->Libraries and add new JAR.)
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>
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!
Have you tried using JSONArray.getJSONObject(int), and JSONArray.length() to create your for-loop:
for (int i = 0; i < recs.length(); ++i) {
JSONObject rec = recs.getJSONObject(i);
int id = rec.getInt("id");
String loc = rec.getString("loc");
// ...
}
An org.json.JSONArray is not iterable.
Here's how I process elements in a net.sf.json.JSONArray:
JSONArray lineItems = jsonObject.getJSONArray("lineItems");
for (Object o : lineItems) {
JSONObject jsonLineItem = (JSONObject) o;
String key = jsonLineItem.getString("key");
String value = jsonLineItem.getString("value");
...
}
Works great... :)
I have tried downloading this and adding it to my project on eclipse as an external file yet an error keeps showing up?
http://www.java2s.com/Code/Jar/j/Downloadjavajsonjar.htm