Below is the json file i am trying to parse.I want to print all key and corresponding values.
{
"A":{
"name":"Ram",
"gender":"male",
"designation":"engineer"
},
"B":{
"name":"Shyam",
"gender":"male",
"designation":"student"
},
"C":{
"name":"Mohan",
"gender":"male",
"designation":"manager"
}
}
I have tried the following code:
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Iterator;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.simple.parser.JSONParser;
public class FetchJsonNested {
public static void main(String args[]) throws FileNotFoundException {
try {
JSONParser jp=new JSONParser();
Object obj=jp.parse(new FileReader("C:\\Users\\DELL\\Documents\\NetBeansProjects\\WaterNetwork\\web\\kusharray.json"));
JSONObject job=(JSONObject)obj;
Iterator < ? > keys = job.keys();
while (keys.hasNext()) {
String key = (String) keys.next();
System.out.println(key);
if (job.get(key) instanceof JSONObject) {
System.out.println(job.get(key));
}
}
} catch(Exception e) {
e.printStackTrace();
}
}
}
I have read stuffs from many site but no one is working like the way i want.I want to print all keys and corresponding values.