Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert JSONObject to Map

I have a JSONObject with some attributes that I want to convert into a Map<String, Object>

Is there something that I can use from the json.org or ObjectMapper?

like image 941
Marco C Avatar asked Feb 04 '14 05:02

Marco C


People also ask

Is a JSON object a map?

A JSONObject is an unordered collection of name/value pairs whereas Map is an object that maps keys to values. A Map cannot contain duplicate keys and each key can map to at most one value.

Can JSON have a map?

You can map the data types of your business model into JSON by using the examples. Data in JSON is either an object or an array. A JSON object is an unordered collection of names and values. A JSON array is an ordered sequence of values.

Can we convert JSON object to string?

A JSONObject has several methods such as get , opt and toString . If you have a JSONObject , you can easily convert it into a String using toString method. If you absolutely need JACKSON, you can try: JSONObject object = ...; ObjectMapper mapper = new ObjectMapper(); String jsonString = mapper.

How do I access a JSON object?

To access the JSON object in JavaScript, parse it with JSON. parse() , and access it via “.” or “[]”.


1 Answers

use Jackson (https://github.com/FasterXML/jackson) from http://json.org/

HashMap<String,Object> result =        new ObjectMapper().readValue(<JSON_OBJECT>, HashMap.class); 
like image 118
A Paul Avatar answered Oct 11 '22 15:10

A Paul