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?
This way its works like a Map...
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://wiki.fasterxml.com/JacksonInFiveMinutes), you'd do:
(where JSON_SOURCE is a File, input stream, reader, or json content String)
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.
One more alternative is json-simple which can be found in Maven Central:
The artifact is 24kbytes, doesn't have other runtime dependencies.
Use JSON lib E.g. http://www.json.org/java/