2

I need to read JsonElements from given JSON files. I am using org.json.simple jar.

Here is the sample of my json:

[{
 "Submission ID": "9938306",
 "Lat": "17.447191666666665",
 "Long": "78.38849"
 },
 {
  "Submission ID": "9938306",
 "Lat": "17.447191666666665",
 "Long": "78.38849"
}]

I wrote this following code to read JsonArray but not able to figure out how to read JsonElements from it:

try {
      JSONParser parser = new JSONParser();
      Object obj = parser.parse(new FileReader("sampleData.json"));
      JSONArray array = (JSONArray) obj;
      Iterator iter = array.iterator();
      while (iter.hasNext()){

      }
    }

How can I read all JSONelements for each JSONarray? For example:

EDIT

I want to iterate all JsonElements in JsonAray. In my given Json I do have submission ID and submission_ID. Key is dynamic in some point and I need to read it and want to apply some regex on it.

Community
  • 1
  • 1
Prerna Rawat
  • 151
  • 1
  • 12
  • Check out [this](http://stackoverflow.com/questions/16574482/decoding-json-string-in-java) post it might help. – Youssef NAIT Jan 21 '16 at 11:35
  • Possible duplicate of [How to parse JSON in Java](http://stackoverflow.com/questions/2591098/how-to-parse-json-in-java) – Evgeniy Mishustin Jan 21 '16 at 11:36
  • Actually they are reading JsonObject by giving `.get("")` function. I want to iterate it to each JsonElement for an each arrays – Prerna Rawat Jan 21 '16 at 11:37
  • @LuciusHipan It's a different question because They are getting JsonElements with the help of "key_name". In my scenario I want to iterate over all of the keys. – Prerna Rawat Jan 21 '16 at 11:41

4 Answers4

3

running code. try it

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) throws FileNotFoundException, IOException, ParseException {

             JSONParser parser = new JSONParser();
              Object obj = parser.parse(new FileReader("/home/stpl/NIKHIL/text.json"));
              JSONArray array = (JSONArray) obj;
              for(int i = 0; i < array.size(); i++)
              {
                 JSONObject objects = (JSONObject)array.get(i);
                 System.out.println(objects.get("Submission ID")+" "+objects.get("Lat")+" "+objects.get("Long"));
              }   
        }
    } 

my text.json

[{
 "Submission ID": "9938306",
 "Lat": "17.447191666666665",
 "Long": "78.38849"
 },
 {
  "Submission ID": "9938306",
 "Lat": "17.447191666666665",
 "Long": "78.38849"
}]
Nikhi K. Bansal
  • 444
  • 2
  • 14
0

You should use Jackson ? Which provides good utilities to parse JSON file.

Bhokal
  • 71
  • 3
0

You can try this code :

JSONParser parser = new JSONParser();
Object obj = parser.parse(new FileReader("d://sample.json"));
JSONArray array = (JSONArray) obj;
Iterator iter = array.iterator();
while (iter.hasNext()) {
    JSONObject json = (JSONObject) iter.next();
    Iterator<String> keys = json.keySet().iterator();
    while (keys.hasNext()) {
        String key = keys.next();
        System.out.println("Key :" + key + "  Value :" + json.get(key));
    }
}

This will iterate each key and value of given JSON.

Sachin Gupta
  • 7,805
  • 4
  • 30
  • 45
0

Please try following code,

try {
      JSONParser parser = new JSONParser();
      Object obj = parser.parse(new FileReader("sampleData.json"));
      JSONArray array = (JSONArray) obj;
      for(int i=0;i<array.length();i++){
          JSONObject obj=array.getJSONObject(i);
         System.out.println(obj.get("Long"));
      }
    } 

Note:not compiled

Bharat DEVre
  • 539
  • 3
  • 13