2

I am very new to JSON. I managed to print out 1 element in the JSON file using Java. However if the file consists of more than 1 element, I do not know how to go about retrieving it. Should I use JSONArray? I tried to search and apply JSONArray, but i got no idea how to. Or does it has something to do with Gson, Jackson thingy? I got no idea what are they..

This is my example.json file:

{"rel": "/r/AtLocation", "weight": 0.5, "dataset": "/d/dbpedia/en", "sources": ["/s/dbpedia/3.7"], "id": "/e/ec1914bfb0606c36376fbbcd316e5666022e2469", "features": ["/c/en/apple_inc /r/AtLocation -", "/c/en/apple_inc - /c/en/unite_state", "- /r/AtLocation /c/en/unite_state"], "end": "/c/en/unite_state", "license": "/l/CC/By-SA", "uri": "/a/[/r/AtLocation/,/c/en/apple_inc/,/c/en/unite_state/]", "start": "/c/en/apple_inc", "context": "/ctx/all", "surfaceText": null}
{"rel": "/r/IsA", "weight": 0.5, "dataset": "/d/dbpedia/en", "sources": ["/s/dbpedia/3.7"], "id": "/e/914e6775fd79d660bacf22ec699568e6694da3e8", "features": ["/c/en/america_beautiful /r/IsA -", "/c/en/america_beautiful - /c/en/national_anthem", "- /r/IsA /c/en/national_anthem"], "end": "/c/en/national_anthem", "license": "/l/CC/By-SA", "uri": "/a/[/r/IsA/,/c/en/america_beautiful/,/c/en/national_anthem/]", "start": "/c/en/america_beautiful", "context": "/ctx/all", "surfaceText": null}

This is my java file:

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 test
{
 public static void main(String[] args)
 {
 JSONParser parser = new JSONParser();
 try
 {
 Object obj = parser.parse(
 new FileReader("C:/Users/LijingYeo/Desktop/example.json"));
 JSONObject jsonObject = (JSONObject) obj;
 String rel = (String) jsonObject.get("rel");
 System.out.println(rel);
 String start = (String) jsonObject.get("start");
 System.out.println(start);
 String end = (String) jsonObject.get("end");
 System.out.println(end);
 }
 catch (FileNotFoundException e)
 {
 e.printStackTrace();
 }
 catch (IOException e)
 {
 e.printStackTrace();
 }
 catch (ParseException e)
 {
 e.printStackTrace();
 }
 }
}

All suggestions and helps rendered are sincerely appreciated by me, thank you for your time and effort!

ThinkingStiff
65.4k31 gold badges148 silver badges241 bronze badges
asked Sep 18, 2012 at 2:16

3 Answers 3

3

I think you should read your json file line by line and then parse to json object. This sample code works for your json file.

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
public class ReadJsonFile
{
 public static void main(String[] args)
 { 
 readJsonFile();
 }
 public static void readJsonFile() {
 BufferedReader br = null;
 JSONParser parser = new JSONParser();
 try {
 String sCurrentLine;
 br = new BufferedReader(new FileReader("D:\\example.json"));
 while ((sCurrentLine = br.readLine()) != null) {
 System.out.println("Record:\t" + sCurrentLine);
 Object obj;
 try {
 obj = parser.parse(sCurrentLine);
 JSONObject jsonObject = (JSONObject) obj;
 String rel = (String) jsonObject.get("rel");
 System.out.println(rel);
 String start = (String) jsonObject.get("start");
 System.out.println(start);
 String end = (String) jsonObject.get("end");
 System.out.println(end);
 } catch (ParseException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 }
 }
 } catch (IOException e) {
 e.printStackTrace();
 } finally {
 try {
 if (br != null)br.close();
 } catch (IOException ex) {
 ex.printStackTrace();
 }
 }
 }
}

Json file is yours as below. I hope this may help.

answered Sep 18, 2012 at 3:48
Sign up to request clarification or add additional context in comments.

2 Comments

Your json file is not json object but writing json objects line by line.
OMG. I thank you for your help @sweson! You make me feel confident to carry on with my work! Cheers to you!
0

Whatever you are doing is right.

Your JSON structure is of type JSONObject, i.e (Key, Value) pairs. Of those elements, features element's value is of type JSONArray. So while browsing through the elements, you should cast the value of features element to JSONArray and then iterate through the list of elements in that array.

JSONArray features = (JSONArray) jsonObject.get("features");
int size = features.size();
for (int i = 0; i < size; i++)
{
 System.out.println(features.get(i));
}

HTH.

answered Sep 18, 2012 at 2:25

6 Comments

Hi @Vikdor, I got this error: The method length() is undefined for the type JSONArray
What JSON parser library are you using? I was referring to JSONArray from json.org/java/index.html. Can you check the equivalent of length() method for the JSONArray in your library? something like size() etc.,?
Sure Sir! I thank you for your help! I will read up and check! I am using json.simple library.. is that the library name you are looking for? I cant really converse well in programming language, especially JSON. ): My bad.. Sorry!
OKay. json.simple's JSONArray has size() method to get the length of the array. Updated the code snippet in my answer accordingly.
size() works, but the print out is abit weird. I have modified my .json file. Just now i only show you 1 element, now there is 2 elements..
|
0

You can use ObjectMapper.

String jsonString = null;
if (prettyPrint == true) {
 // this is time consuming
 jsonString = myObjectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(myObject);
} else {
 // faster option
 jsonString = myObjectMapper.writeValueAsString(myObject);
}
answered Sep 18, 2012 at 4:07

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.