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.

4
  • Check out this post it might help. Jan 21, 2016 at 11:35
  • Possible duplicate of How to parse JSON in Java Jan 21, 2016 at 11:36
  • Actually they are reading JsonObject by giving .get("") function. I want to iterate it to each JsonElement for an each arrays Jan 21, 2016 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. Jan 21, 2016 at 11:41

4 Answers 4

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"
}]
0

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

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.

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

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.