how to iterate through json objects in java

I am trying to iterate through my json file and get required details here is my json

{
"000": {
    "component": "c",
    "determinantType": "dt",
    "determinant": "d",
    "header": "h",
    "determinantvalue": "null"
},
"001": {
    "component": "t",
    "determinantType": "i",
    "determinant":"ld",
    "header": "D",
    "determinantvalue": "null"
},
"002": {
    "component": "x",
    "determinantType": "id",
    "determinant": "pld",
    "header": "P",
    "determinantValue": "null"
}}

my java code

FileReader file = new FileReader("test.json");
Object obj = parser.parse(file);
System.out.println(obj);
JSONObject jsonObject = (JSONObject) obj;            
JSONArray msg = (JSONArray) jsonObject.get(key);          
Iterator<String> iterator = msg.iterator();         
while (iterator.hasNext()) {
System.out.println(iterator.next());            
String component = (String) jsonObject.get("component");           
System.out.println("component: " + component);           

As you can see in the code I am importing my json file and trying to get next elements and printing components out of it , I should also print header,determinant and determinant value as well Thank you

Jon Skeet
people
quotationmark

You don't have an array - you have properties with names of "000" etc. An array would look like this:

"array": [ {
    "foo": "bar1",
    "baz": "qux1"
  }, {
    "foo": "bar2",
    "baz": "qux2"
  }
]

Note the [ ... ] - that's what indicates a JSON array.

You can iterate through the properties of a JSONObject using keys():

// Unfortunately keys() just returns a raw Iterator...
Iterator keys = jsonObject.keys();
while (keys.hasNext()) {
    Object key = keys.next();
    JSONObject value = jsonObject.getJSONObject((String) key);
    String component = value.getString("component");
    System.out.println(component);
}

Or:

@SuppressWarnings("unchecked")
Iterator<String> keys = (Iterator<String>) jsonObject.keys();
while (keys.hasNext()) {
    String key = keys.next();
    JSONObject value = jsonObject.getJSONObject(key);
    String component = value.getString("component");
    System.out.println(component);
}

people

See more on this question at Stackoverflow