arrays - How can I write a multidimensional JSON object in JSP and pass the JSON object back to JavaScript? -
i trying write multidimensional object or array in jsp , pass ajax call javascript. now, decoding json object using ajax have figured out (jquery json). so, i'm on front.
but lost on creating multidimensional json object or array in jsp.
i have been looking @ json-simple json plugin jsp (http://code.google.com/p/json-simple/). , it's not plugin, have been unable find examples of multidimensional json objects or array examples in jsp.
like, example it's one-dimensional:
//import org.json.simple.jsonobject; jsonobject obj=new jsonobject(); obj.put("name","foo"); obj.put("num",new integer(100)); obj.put("balance",new double(1000.21)); obj.put("is_vip",new boolean(true)); obj.put("nickname",null); system.out.print(obj);
i json object have result this:
{ "results": [ { "address_components": [ { "long_name": "1600", "short_name": "1600", "types": [ "street_number" ] } ], } ] }
then, i'd pass javascript , decode it.
to sum up: how can create multidimensional object or array in jsp? json plugin inconsequential; whatever works, i'll more happy.
i appreciate help. thank in advance.
to create structure using gson:
{ "results": [ { "address_components": [ { "long_name": "1600", "short_name": "1600", "types": [ "street_number" ] } ], } ] }
you this:
<% string[] types = {"street_number"}; hashtable address_components = new hashtable(); address_components.put("long_name", 1600); address_components.put("short_name", 1600); address_components.put("types", types); hashtable results = new hashtable(); results.put("address_components", address_components); // make sure have gson jar in classpath // (place in tomcat/classes directory) com.google.gson.gson gson = new com.google.gson.gson(); string json = gson.tojson(obj); out.print(json); %>
Comments
Post a Comment