cy
2022-06-21 129904537f66509f97b285e7eb4f42b3dc349dd0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package cn.ksource.core.util;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
 
import org.apache.commons.lang.StringUtils;
 
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.util.JSONUtils;
 
public class JsonUtil {
    
    /**
     * 将JSON格式的字符串转化为Map对象,只进行第一层次的转换
     * @param json
     * @return
     * 作者:杨凯
     */
    public static Map<String,String> json2SimpleMap(String json){
        JSONObject object = JSONObject.fromObject(json);
        Map map = new HashMap();
        Iterator it = object.keys();
        while (it.hasNext()) {
            String key = (String)it.next();
            map.put(key, ConvertUtil.obj2Str(object.get(key)));
        }
        return map;
    }
    
    /**
     * 将JSON格式的字符串转化为Map对象,进行所有层次的转换
     * @param json
     * @return
     * 作者:杨凯
     */
    public static Map<String, Object> json2Map(String json){
        if(StringUtil.isEmpty(json)){
            return null;
        }
        JSONObject object = JSONObject.fromObject(json);
        Map map = new HashMap();
        Iterator it = object.keys();
        while (it.hasNext()) {
            String key = (String)it.next();
            String value = object.getString(key);
            //如果为Map结构
            if (JSONUtils.isObject(object.get(key))) {
                map.put(key, json2Map(value));
            //如果为List结构
            } else if (JSONUtils.isArray(object.get(key))) {
                List<Map> list = new ArrayList<Map>();
                JSONArray jArray = JSONArray.fromObject(value);
                for (int i = 0; i < jArray.size(); i++) {
                    list.add(json2Map(jArray.getString(i)));
                }
                map.put(key, list);
            } else {
                map.put(key, ConvertUtil.obj2Str(object.get(key)));
            }
        }
        return map;
    }
    
    /**
     * 将JSON格式的字符串转化为List对象,进行所有层次的转换
     * @param json
     * @return
     * 作者:杨凯
     */
    public static List<Map> json2List(String json){
        if (StringUtils.isBlank(json)) {
            return new ArrayList<Map>();
        }
        
        JSONArray jArray = JSONArray.fromObject(json);
        List<Map> list = new LinkedList<Map>();
        for (int i = 0; i < jArray.size(); i++) {
            list.add(json2Map(jArray.getString(i)));
        }
        return list;
    }
    
    /**
     * 将JSON格式的字符串转化为List对象,只进行第一层次的转换
     * @param json
     * @return
     * 作者:杨凯
     */
    public static List<Map> json2SimpleList(String json){
        JSONArray jArray = JSONArray.fromObject(json);
        List<Map> list = new LinkedList<Map>();
        for (int i = 0; i < jArray.size(); i++) {
            list.add(json2SimpleMap(jArray.getString(i)));
        }
        return list;
    }
    
    public static String map2Json(Map map){
        JSONObject jsonObject = new JSONObject();
        jsonObject.putAll(map == null || map.isEmpty() ? new HashMap() : map);
        return jsonObject.toString();
    }
    
    public static String list2Json(List list){
        JSONArray jsonArray = JSONArray.fromObject(list == null || list.isEmpty() ? new ArrayList() : list);
        return jsonArray.toString();
    }
    
    /**
     * 功能描述:将对象转换为JSON格式的字符串<BR>
     * @param obj
     * @return
     * @author:杨凯<BR>
     * 时间:May 22, 2009 12:18:26 PM<BR>
     */
    public static String obj2Json(Object obj) {
        JSONArray jsonArray = JSONArray.fromObject(obj);
        return jsonArray.toString();
    }
    public static String obj2JsonObj(Object obj) {
        JSONObject jsonArray = JSONObject.fromObject(obj);
        return jsonArray.toString();
    }
    
    public static void main(String[] args) {
        Map map = new HashMap();
        map.put("name", "yangkai");
        map.put("age", 20);
        
        List<Map> list = new ArrayList<Map>();
        Map tempMap = new HashMap();
        tempMap.put("key1", "value1");
        tempMap.put("key2", "value2");
        
        Map tempMap1 = new HashMap();
        tempMap1.put("key3", "value3");
        tempMap1.put("key4", "value4");
        list.add(tempMap);
        list.add(tempMap1);
        map.put("list", list);
        String json = JsonUtil.map2Json(map);
        System.out.println(json);
//        json2List(list2Json(list));
        
    }
}