0

Hii i am having trouble to read .json file in Java containing following structure,

{"id":"1", "name":"A", "address":{"plot no":"22", "street":"road"}}
{"id":"2", "name":"A", "address":{"plot no":"22", "street":"road"}}
{"id":"3", "name":"A", "address":{"plot no":"22", "street":"road"}}
{"id":"4", "name":"A", "address":{"plot no":"22", "street":"road"}}

I have such 10k records. I cant change structure. I want to read it and do processing on "address". I need an efficient way to read it and fetch only address. Any suggestions?

2 Answers2

0

yourJsonObject is your json file extracted in java as a json object. And this:

JSONObject jso = yourJsonObject.getJSONObject("address") ; 

will extract your "address" part as a json object. Then you can do all the classical processing on it.

Souin
  • 94
  • 1
  • 11
0

You can use JSON simple library,i hope you take idea with this example ;

    JSONParser parser = new JSONParser();
    JSONArray a = (JSONArray) parser.parse(new FileReader("file name"));

    for (Object o : a){
      JSONObject person = (JSONObject) o;

      String name = (String) person.get("name");
      System.out.println(name);

      String city = (String) person.get("city");
      System.out.println(city);

      String job = (String) person.get("job");
      System.out.println(job);

     JSONArray cars = (JSONArray) jsonObject.get("cars");

     for (Object c : cars)
       {
        System.out.println(c+"");
        }
     }
SerefAltindal
  • 339
  • 3
  • 12