0

I have a big JSON file(demo.json). Here is how it looks like:

{  
   "preview":false,
   "result":{  
      "search_term":"rania",
      "request_time":"Sat Apr 01 12:47:04 -0400 2017",
      "request_ip":"127.0.0.1",
      "stats_type":"stats",
      "upi":"355658761",
      "unit":"DR",
      "job_title":"Communications Officer",
      "vpu":"INP",
      "organization":"73",
      "city":"Wash",
      "country":"DC",
      "title":"Tom",
      "url":"www.demo.com",
      "tab_name":"People-Tab",
      "page_name":"PEOPLE",
      "result_number":"5",
      "page_num":"0",
      "session_id":"df234f468cb3fe8be",
      "total_results":"5",
      "filter":"qterm=rina",
      "_time":"2017-04-01T12:47:04.000-0400"
   }
}
{"preview"......}
{"preview"......}
....

I would like to access search term and page_name which is inside of the result and convert them into the string . Below is my java code which is not working:

BufferedReader br = new BufferedReader(new FileReader("demo.json"));
String line;
while ((line = br.readLine()) != null) {

                JSONParser parser = new JSONParser();
                Object obj = parser.parse(line);
                JSONObject jsonObject = (JSONObject) obj;
                String searchterm= (String) jsonObject.get("search_term");
                String page_name = (String) jsonObject.get("page_name");
}

I am not familiar with how to access the nested fields and convert those into string. Any help is appreciated.

2
  • You're trying to parse each line. But { is not valid JSON. "preview":false, is not valid JSON either. Commented May 2, 2017 at 19:05
  • My JSON is valid. The one I posted was just for example. I want to know how to access the nested field search_term which is inside result Commented May 2, 2017 at 19:06

3 Answers 3

1
boolean preview = jsonObject.get("preview");
JSONObject result = jsonObject.getJSONObject("result");
String search_term = result.getString("search_term");
String page_name = result.getString("page_name");
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the reply. Do I have to add any dependency in maven? I am getting an error on getJSONObject "getJSONObject(String) is undefined for the type JSONObject"
No you should just import org.json.JSONObject; stleary.github.io/JSON-java/org/json/JSONObject.html
I used the specified import, but I am getting an error "The import org.json.JSONObject cannot be resolved ". I have a maven dependency mvnrepository.com/artifact/com.googlecode.json-simple/… for another part of the program. Do you think this might be a problem?
Cha. Wrong library. Here's your repo. mvnrepository.com/artifact/org.json/json/20160810
0

You can use library like Gson .Convert the data into Map

    BufferedReader br = new BufferedReader(new FileReader("demo.json"));
String line;
StringBuilder builder=new StringBuilder();
while ((line = br.readLine()) != null) {
builder.append(line);

} 

       Type type = new TypeToken<Map<String, String>>(){}.getType();
       Map<String, String> myMap = gson.fromJson(builder.toString(), type);

You can use myMap to get Values for json Keys . For nested Key you can another convert it to Map again.

Comments

0

Use ObjectMapper to convert your JSON to PoJo and then use it.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.