Parse Hashtable in JSON, perfect(jsonencode). But I don't know how to parse json in Hashtable(jsondecode). (Android) -


i've created json encode enter hashtable (public hashtable<?, ?> jsondecode(string data) {... return objjs.tostring(); } ) , string in json format. is:

if have hashtable (hashtable in hashtable):

example hashtable:

hashtable<string, object> exampleht = new hashtable<string, object>();     exampleht.put("color", "red");     exampleht.put("otherkey", "othervalue");     exampleht.put("otherkey2", "othervalue2");  hashtable<string, object> country = new hashtable<string, object>();     country.put("spain", "madrid");     country.put("france","paris");     country.put("italy", "rome");  hashtable<string, string> pokemon = new hashtable<string, string>();     pokemon.put("pikachu", "electric");     pokemon.put("charmander","fire");  country.put("pokemons", pokemon);  exampleht.put("countries", country); 

i use function(jsonencode(exampleht);) , string:

{     "color":"red",     "countries":{         "spain":"madrid",         "france":"paris",         "italy":"rome",         "pokemons":{             "pikachu":"electric",             "charmander":"fire"         }     },     "otherkey":"othervalue",     "otherkey2":"othervalue2" } 

it works perfectly! problem create reverse process, jsondecode.

hashtable<?, ?> hashunknown = jsondecode(jsonstringexample);  public hashtable<?, ?> jsondecode(string data) {  // not know how parse json in hashtable, without indicating tags manually. 

}

i not know how parse json in hashtable, without indicating tags manually. is, without it:

jsonarray menuobject = new jsonarray (jobject.getstring ("color")); jsonarray menuobject = new jsonarray (jobject.getstring ("countries")); 

  should dynamic without knowing json content without writing manually color, countries, ....

any ideas or advice? thanks,

you can iterator object (java.util.iterator) on keys of jsonobject (jobject) can write this:

iterator<string> = jobject.keys(); string key = null; object value = null; while (it.hasnext()) {     key = it.next();     value = jobject.get(key);     // test instance of value variable     // , perform logic } 

Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -