//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