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?
My post could be helpful for others, so imagine you have a map with a specific object in values, something like that:
To parse this JSON file with GSON library, it's easy : if your project is mavenized
Then use this snippet :
The corresponding POJO should be something like that :
Hope it helps !
The JsonTools library is very complete. It can be found at Github.
I like google gson library.
When you don't know structure of json. You can use
and then you can work with json. e.g. how to get "value1" from your gson:
I do it this way. It's Simple.
Underscore-java library can convert json string to hash map. I am the maintainer of the project.
Code example: