How to Convert a JSON String to a Map<String, String> with Jackson JSON

How to convert a JSON string to a Map String, String with Jackson JSON

[Update Sept 2020] Although my original answer here, from many years ago, seems to be helpful and is still getting upvotes, I now use the GSON library from Google, which I find to be more intuitive.

I've got the following code:

public void testJackson() throws IOException {  
ObjectMapper mapper = new ObjectMapper();
File from = new File("albumnList.txt");
TypeReference<HashMap<String,Object>> typeRef
= new TypeReference<HashMap<String,Object>>() {};

HashMap<String,Object> o = mapper.readValue(from, typeRef);
System.out.println("Got " + o);
}

It's reading from a file, but mapper.readValue() will also accept an InputStream and you can obtain an InputStream from a string by using the following:

new ByteArrayInputStream(astring.getBytes("UTF-8")); 

There's a bit more explanation about the mapper on my blog.

Convert JSON to Map String, String using Jackson

Change your code to:

...

Map<String, Object> map = responseBodyMapper.readValue(responseBody, Map.class);
ArrayList<Map<String, Object>> innerDataMap = (ArrayList<Map<String, Object>>) map.get("dados");
int responseCount = Integer.parseInt(String.valueOf(map.get("quantidade")));

...

Your top level object is more than just Map<String, Object> as it also has lists and primitives (string).

Unable to convert Json string to Map String, Object

This is what causing your problem

request = objectMapper.writeValueAsString(request);

It affects your String json, just pass the String object to the readValue method, it accepts also strings

Convert a JSON String to a HashMap

In recursive way:

public static Map<String, Object> jsonToMap(JSONObject json) throws JSONException {
Map<String, Object> retMap = new HashMap<String, Object>();

if(json != JSONObject.NULL) {
retMap = toMap(json);
}
return retMap;
}

public static Map<String, Object> toMap(JSONObject object) throws JSONException {
Map<String, Object> map = new HashMap<String, Object>();

Iterator<String> keysItr = object.keys();
while(keysItr.hasNext()) {
String key = keysItr.next();
Object value = object.get(key);

if(value instanceof JSONArray) {
value = toList((JSONArray) value);
}

else if(value instanceof JSONObject) {
value = toMap((JSONObject) value);
}
map.put(key, value);
}
return map;
}

public static List<Object> toList(JSONArray array) throws JSONException {
List<Object> list = new ArrayList<Object>();
for(int i = 0; i < array.length(); i++) {
Object value = array.get(i);
if(value instanceof JSONArray) {
value = toList((JSONArray) value);
}

else if(value instanceof JSONObject) {
value = toMap((JSONObject) value);
}
list.add(value);
}
return list;
}

Using Jackson library:

import com.fasterxml.jackson.databind.ObjectMapper;

Map<String, Object> mapping = new ObjectMapper().readValue(jsonStr, HashMap.class);

Convert JSON to 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 https://github.com/FasterXML/jackson-databind/#5-minute-tutorial-streaming-parser-generator), you'd do:

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

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

how to convert json to list/map with jackson

You can use Jackson ObjectMapper with TypeReference ,
First you need to read it as Map<String,Object> then you can extract the name and num with Java.

you need to get the data from obj then for each item in the dataList you have to map it to a new HashMap as shown below

ObjectMapper objectMapper = new ObjectMapper ();
Map<String,Object> jsonMap = objectMapper.readValue(jsonData,new TypeReference<Map<String,Object>>(){});
List<Object> data = ((Map<String,Object>)jsonMap.get("obj")).get("data");
List<Map<String,String> result = data.stream()
.map(d->(Map<String,Object> d)
.map(d->{
Map<String,String> map = new HashMap();
map.put(d.get("name"),((Map<String,String>)d.get("value")).get("num"));
return map;
})
.collect(Collectors.toList());

However if you can create a class for the the data it will be bit easier.



Related Topics



Leave a reply



Submit