-1

//External jsonArray file

{

    "items": [
        {
            "index": 10,
            "index_start_at": 56,
            "integer": 12,
            "float": 16.8248,
            "Firstname": "Natalie",
            "surname": "MacDonald",
            "fullname": "Hilda Rich",
            "email": "eva@durham.jp",
            "Zip": 30988
        },
        {
            "index": 2,
            "index_start_at": 57,
            "integer": 5,
            "float": 13.8932,
            "Firstname": "Jeff",
            "surname": "Miles",
            "fullname": "Meredith Wall",
            "email": "herbert@green.af",
            "Zip": 47888
        },
        {
            "index": 3,
            "index_start_at": 58,
            "integer": 14,
            "float": 10.1125,
            "Firstname": "Mary",
            "surname": "Huff",
            "fullname": "George Schroeder",
            "email": "martha@waller.bo",
            "Zip": 3985
         }
    ]

}

how to get keys from above jsonArray and storing those in some array and then randomize the values of those keys in java?? Edited CODE...

     import java.io.FileNotFoundException;
        import java.io.FileReader;
        import java.io.IOException;
        import java.util.Iterator;

        import org.json.JSONException;
        import org.json.JSONObject;
        import org.json.simple.JSONArray;
        import org.json.simple.parser.JSONParser;
        import org.json.simple.parser.ParseException;




        public class JSONReadFromFile {


            public static void main(String[] args) throws JSONException {

                JSONParser parser = new JSONParser();


                String jsonString=null;

                Object Obj;

                //JSONObject element;

                try {

                    Obj = parser.parse(new FileReader("jsonArray.json"));
                    System.out.println(Obj);
                    jsonString=Obj.toString();

                    JSONObject object = new JSONObject(jsonString); //jsonString = String from the file
                    org.json.JSONArray array = object.getJSONArray("items");
                    Iterator<Object> iterator = array.iterator();
                    while(iterator.hasNext()){
                        JSONObject jsonObject = (JSONObject) iterator.next();
                        for(String key : jsonObject.keySet()){
                            System.out.println(key + ":" + jsonObject.get(key));
                        }
                    }

        }

}
        catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (ParseException e2) {
            // TODO Auto-generated catch block
            e2.printStackTrace();
        } 

    }
}

I am trying to do it like this .. is this correct way of doing ?? Firstly i am reading the json file and then extracting the keys from it. here in above code i am getting two errors---- The method iterator is undefined for the type jsonArray && The method keyset is undefined for the type jsonArray

Robert Dresler
  • 10,580
  • 2
  • 22
  • 40
ASM
  • 27
  • 1
  • 1
  • 9

1 Answers1

2

You can do it with two loops, e.g.:

JSONObject object = new JSONObject(jsonString); //jsonString = String from the file
JSONArray array = object.getJSONArray("items");
Iterator<Object> iterator = array.iterator();
while(iterator.hasNext()){
    JSONObject jsonObject = (JSONObject) iterator.next();
    for(String key : jsonObject.keySet()){
        System.out.println(key + ":" + jsonObject.get(key));
    }
}

Update

Here's the full example with all the imports:

import java.util.Iterator;
import org.json.JSONArray;
import org.json.JSONObject;

public class Test {

    public static void main(String[] args) {

        JSONObject object = new JSONObject("{\"items\":[{\"index\":10}]}");
        JSONArray array = object.getJSONArray("items");
        Iterator<Object> iterator = array.iterator();

        while (iterator.hasNext()) {
            JSONObject jsonObject = (JSONObject) iterator.next();

            for (String key : jsonObject.keySet()) {
                System.out.println(key + ":" + jsonObject.get(key));
            }
        }

    }
}
philip
  • 484
  • 9
  • 26
Darshan Mehta
  • 30,102
  • 11
  • 68
  • 102
  • and how to get value of corresponding keys and then randomize the values of keys each time. – ASM Sep 18 '17 at 07:36
  • `jsonObject.get(key)` will give you the value of a particular key. Randomizing depends on what/how you want to randomize. – Darshan Mehta Sep 18 '17 at 07:38
  • I want the randomize the values in range of 1 to 1000. – ASM Sep 18 '17 at 07:39
  • You can use Java's `Random` method to randomize the values, have a look at [this](https://stackoverflow.com/questions/5271598/java-generate-random-number-between-two-given-values) SO answer for examples. – Darshan Mehta Sep 18 '17 at 07:41
  • I am getting the error as :The method iterator() is undefined for the type JSONArray and The method keySet() is undefined for the type JSONObject. how to resolve these errors – ASM Sep 18 '17 at 08:50
  • Are you using `org.json` version `20170516`? Looks like you are just missing the `import` statements. – Darshan Mehta Sep 18 '17 at 09:33
  • included these imports import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.Iterator; import org.json.JSONException; import org.json.JSONObject; import org.json.simple.JSONArray; import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; – ASM Sep 18 '17 at 09:37
  • Could you post the full snippet in the question? – Darshan Mehta Sep 18 '17 at 09:38
  • @ASM try changing `import org.json.simple.JSONArray;` to `import org.json.JSONArray;`? – Darshan Mehta Sep 18 '17 at 09:52
  • In fact, you can remove `simple` package from all the imports. – Darshan Mehta Sep 18 '17 at 09:53
  • @ASM updated the answer with full example. Can you copy paste it as is and see if that works fine? – Darshan Mehta Sep 18 '17 at 10:22
  • its till wrong because iterators works on collections and array is not a collection. – ASM Sep 18 '17 at 10:29
  • This answer can not be more wrong. – SharadxDutta Oct 21 '21 at 14:05
  • There is a mismatch in the imports, how did you run this code? – SharadxDutta Oct 21 '21 at 14:06