Java Json to Object json2map(String jsonStr)

Here you can find the source of json2map(String jsonStr)

Description

json string convert to map

License

Open Source License

Declaration

public static <T> Map<String, Object> json2map(String jsonStr) throws Exception 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.HashMap;

import java.util.Map;

public class Main {
    private final static ObjectMapper objectMapper = new ObjectMapper();

    /**/*w  w w.  j av  a2 s.c om*/
     * json string convert to map
     */
    public static <T> Map<String, Object> json2map(String jsonStr) throws Exception {
        return objectMapper.readValue(jsonStr, Map.class);
    }

    /**
     * json string convert to map with javaBean
     */
    public static <T> Map<String, T> json2map(String jsonStr, Class<T> clazz) throws Exception {
        Map<String, Map<String, Object>> map = objectMapper.readValue(jsonStr, new TypeReference<Map<String, T>>() {
        });
        Map<String, T> result = new HashMap<String, T>();
        for (Map.Entry<String, Map<String, Object>> entry : map.entrySet()) {
            result.put(entry.getKey(), map2pojo(entry.getValue(), clazz));
        }
        return result;
    }

    /**
     * map convert to javaBean
     */
    public static <T> T map2pojo(Map map, Class<T> clazz) {
        return objectMapper.convertValue(map, clazz);
    }
}

Related

  1. deserialize(String json, Class targetType)
  2. deserialize(String jsonData, Class typeOfT)
  3. deserializeFromDataNode(JsonParser jp, DeserializationContext ctxt, String propertyName, TypeReference typeReference)
  4. deserializeJson(String content, Class valueType)
  5. json2List(String json, Class beanClass)
  6. json2pojo(String jsonStr, Class clazz)
  7. jsonBigDecimal(JsonValue value)
  8. jsonStringToList(String jsonArrStr, Class clazz)
  9. jsonToBeanDateSerializer(String jsonStr, Class cl, final String pattern)