Monday, September 30, 2013

Get json object data without knowing the keys :)

Dynamic Json Object Keyset Finding



import java.util.Iterator;
import java.util.Set;

import org.json.simple.JSONObject;

public class Json {
public static void main(String[] args) {
	JSONObject jsonObject = new JSONObject();
	jsonObject.put("a", "aaa");
	jsonObject.put("b", "bbb");
	
    Set keys = jsonObject.keySet();
    Iterator a = keys.iterator();
    while(a.hasNext()) {
    	String key = (String)a.next();
        // loop to get the dynamic key
        String value = (String)jsonObject.get(key);
        System.out.print("key : "+key);
        System.out.println(" value :"+value);
    }
}
}

4 comments:

  1. @Devan,

    Can you please help me iterate thru the "value" if it is an Object itself ... so basically get the "keys" for nested JSON.

    Best,
    Jigs

    ReplyDelete

Total Pageviews

Translate