简体   繁体   中英

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

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);
}

There's not a JSONArray , only a few JSONObjects . Iterate the keys of the main JSONObject with JSONObject.keys() .

public static final String COMPONENT = "component";
public static final String DT = "determinantType";
public static final String D = "determinant": "d";
public static final String HEADER = "header";
public static final String DV = "determinantvalue";

JSONObject jso = getItFromSomewhere();
for (Object key : jso.keys()) {
    JSONObject subset = jso.getJSONObject(key);
    String d = subset.getString(D);
    String header = subset.getString(HEADER);
    String dv = subset.getString(DV);
    System.out.println(key + " " + header + " " + d + " " + dv);
}

try this...

FileReader file = new FileReader("test.json");
Object obj = parser.parse(file);
System.out.println(obj);
JSONObject jsonObject = (JSONObject) obj;            

Iterator<String> iterator = jsonObject .iterator();  

for(Iterator iterator = jsonObject.keySet().iterator(); iterator.hasNext();) {
    String key = (String) iterator.next();
    System.out.println(jsonObject.get(key));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM