zoukankan      html  css  js  c++  java
  • json转换成Map

    1.如果转换的是Map、或者是简单的对象

    package com.gc.action;
    
    import java.util.Map;
    
    import net.sf.json.JSONObject;
    
    /**
     * 1.json string 转换为 map
     * 2.json string 转换为 对象
     * @author zhangtengda
     *
     */
    public class JsonUtils {
        public static void main(String[] args) {
            String jsonStr = "{"status":"0","data":{"name":"tom","age":18}}";
    
            // ================================
            // 1.如果是拿出来最外层的 map
            Map<Object, Object> result = jsonToMap(jsonStr);
            System.out.println(result.get("status"));
            System.out.println(result.get("data"));
    
            // 2.如果进步一拿到内层的 map
            Map<Object, Object> data = jsonToMap(result.get("data"));
            System.out.println(data.get("name"));
            System.out.println(data.get("age"));
            // ================================
    
            Long beginTime = System.currentTimeMillis();
            // 3.转换为对象
            String personStr = "{"id":12,"name":"mary"}";
            Person person = jsonToBean(personStr, Person.class);
            System.out.println(person);
    
            System.out.println("耗时:"  +( System.currentTimeMillis() - beginTime));
    
        }
    
        /**
         * json string 转换为 map 对象
         * @param jsonObj
         * @return
         */
        public static Map<Object, Object> jsonToMap(Object jsonObj) {
            JSONObject jsonObject = JSONObject.fromObject(jsonObj);
            Map<Object, Object> map = (Map)jsonObject;
            return map;
        }
    
        /**json string 转换为 对象
         * @param jsonObj
         * @param type
         * @return
         */
        public  static <T>  T jsonToBean(Object jsonObj, Class<T> type) {
            JSONObject jsonObject = JSONObject.fromObject(jsonObj);
            T obj =(T)JSONObject.toBean(jsonObject, type);
            return obj;
        } 
    
    }
  • 相关阅读:
    CF1391D 【505】
    CF1389C 【Good String】
    CF1364C 【Ehab and Prefix MEXs】
    CF1353E 【K-periodic Garland】
    CF1349A 【Orac and LCM】
    CF1352C 【K-th Not Divisible by n】
    CF413D 【2048】
    CF257B 【Playing Cubes】
    CF267A 【Subtractions】
    2018.8.16提高B组模拟考试
  • 原文地址:https://www.cnblogs.com/zhao-shan/p/9045367.html
Copyright © 2011-2022 走看看