0

I am trying find some vin data from http://developer.edmunds.com/ using https://api.edmunds.com/v1/api/toolsrepository/vindecoder?vin=1GCEC19C17Z275639&fmt=json&api_key=my_api_key.... I am getting a full json data. I am using simplest way to read json from a URL in java JSONParser and my return data looks like http://pastebin.com/30jXEGvN this.

What is the best way to print all keys and its values from a jsonobject ?

I tried

Iterator<?> keys = object.keys();
        while( keys.hasNext() ){
            String key = (String)keys.next();
            try {
                if( object.get(key) instanceof JSONObject ){
                    detailsTextOut.append(key);
                }
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

But its not working for me :( , any clues ?

Community
  • 1
  • 1
Achayan
  • 5,720
  • 2
  • 39
  • 61

2 Answers2

0

Use the method getNames() to get all the keys (field name) of JSON object.

JSONObject api

String[] keys = JSONObject.getNames(jsonObject);

for (String key : keys) {
    //then get value by key (field name)
    Object obj = jsonObject.get(key);
    System.out.println(key + "-" + obj.toString());
}
Wundwin Born
  • 3,467
  • 19
  • 37
0

Using Jackson ObjectMapper:

    String s = "replace_with_your_json_string";
    ObjectMapper mapper = new ObjectMapper();
    Map<String, Object> values = (Map<String, Object>) mapper.readValue(s, Map.class);
    iterate(values, "");

    private void iterate(Map<String, Object> map, String indent) {
            for(Map.Entry<String, Object> o : map.entrySet()) {
                    System.out.println(indent + o.getKey());
                    if(o.getValue() instanceof Map) {
                            iterate((Map<String, Object>) o.getValue(), indent + "\t");
                    }
                    if(o.getValue() instanceof List) {
                            iterate((List<Object>) o.getValue(), indent + "\t");
                    }
                    else if(o.getValue() instanceof String) {
                            System.out.println(indent + "\t" + o.getValue());
                    }
                    else if(o.getValue() instanceof Number) {
                            System.out.println(indent + "\t" + o.getValue());
                    }
            }
    }

    private void iterate(List<Object> list, String indent) {
            for(Object o : list) {
                    if(o instanceof Map) {
                            iterate((Map<String, Object>) o, indent + "\t");
                    }
                    if(o instanceof List) {
                            iterate((List<Object>) o, indent + "\t");
                    }
                    else if(o instanceof String) {
                            System.out.println(indent + "\t" + o);
                    }
                    else if(o instanceof Number) {
                            System.out.println(indent + "\t" + o);
                    }
            }
    }
Jeesmon
  • 389
  • 1
  • 7