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

5 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
  2. I'm grateful for the wisdom and insight your blog provides – it's like having a wise mentor by my side! Challenge yourself to conquer the rooftops and reign supreme in Rooftop Snipers Game – it's time to dominate!

    ReplyDelete