I'm using Java, and I have a String which is JSON:
{
"name" : "abc" ,
"email id " : ["abc@gmail.com","def@gmail.com","ghi@gmail.com"]
}
Then my Map in Java:
Map<String, Object> retMap = new HashMap<String, Object>();
I want to store all the data from the JSONObject in that HashMap.
Can anyone provide code for this? I want to use the org.json
library.
The following parser reads a file, parses it into a generic
JsonElement
, using Google'sJsonParser.parse
method, and then converts all the items in the generated JSON into a native JavaList<object>
orMap<String, Object>
.Note: The code below is based off of Vikas Gupta's answer.
GsonParser.java
FileLoader.java
Imagine u have a list of email like below. not constrained to any programming language,
Now following is JAVA code - for converting json to map
This is an old question and maybe still relate to someone.
Let's say you have string HashMap
hash
and JsonObjectjsonObject
.1) Define key-list.
Example:
2) Create foreach loop, add
hash
fromjsonObject
with:That's my approach, hope it answer the question.
You can use google gson library to convert json object.
https://code.google.com/p/google-gson/
Other librarys like Jackson are also available.
This won't convert it to a map. But you can do all things which you want.
Using json-simple you can convert data JSON to Map and Map to JSON.