views:

3267

answers:

4
+4  Q: 

Json to Map

Hi, What is the best way to convert a JSON code as this:

{ "data" : { "field1" : "value1", "field2" : "value2"}}

in a Java Map in which one the keys are (field1, field2) and the values for those fields are (value1, value2).

Any ideas? Should I use Json-lib for that? Or better if I write my own parser? Thanks in advance.

+10  A: 

I had success doing this with the tools here: http://www.json.org/java/

Steve McLeod
+1  A: 

The JsonTools library is very complete. It can be found at Berlios.

Bruno Ranschaert
+4  A: 

I hope you were joking about writing your own parser. :-)

For such a simple mapping, most tools from http://json.org (section java) would work. For one of them (Jackson, http://jackson.codehaus.org/Tutorial), you'd do:

HashMap<String,Object> result =
        new ObjectMapper().readValue(JSON_SOURCE, HashMap.class);

(where JSON_SOURCE is a File, input stream, reader, or json content String)

StaxMan
A: 

JSON to Map always gonna be a string/object data type. i haved GSON lib from google.

works very well and JDK 1.5 is the min requirement.