Using JSON to return a Java map
What's the best way to return a Java map using JSON format? My specific need is a key -> value relationship between date and number.
My concern is this: my structure basically contains N elements of the same type (mapping between date and number) and I want to be able to iterate over them quickly via Javascript.
In XML I would:
<relation date='mydate' number='mynumber'/>
<relation date='mydate' number='mynumber'/>
...
<relation date='mydate' number='mynumber'/>
and I would use jQuery like this:
$(xml).find("relation").each(function() {
$(this).attr("date"); // the date
$(this).attr("number"); // the number
})
This is my first experience with JSON and I would like to know if I can do something similar.
a source to share
Although I haven't tried it myself, the Java implementation of JSON from json.org has a constructor .JSONObject
JSONObject(Map)
Once the JSON object is created from Map
, then the JSON string can be retrieved by calling the toString
.
a source to share
String myJson = "{ ";
for (String key : myMap.keySet())
myJson += key + " : " + "'" + myMap.get(key) + "',";
myJson += " } ";
I leave the last comma because it won't give us much trouble. Javascript just ignores it.
Well this will answer your question, but I guess it won't help. Try posting a more specific version.
a source to share