jsonUtil类

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class JsonUtil{
	    public static Map<String, Object> parseJSON2Map(JSONObject json) {
        Map<String, Object> map = new HashMap<String, Object>();
        // 最外层解析
        for (Object k : json.keySet()) {
            Object v = json.get(k);
            // 如果内层还是json数组的话,继续解析
            if (v instanceof JSONArray) {
                List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
                Iterator<JSONObject> it = ((JSONArray) v).iterator();
                while (it.hasNext()) {
                    JSONObject json2 = it.next();
                    list.add(parseJSON2Map(json2));
                }
                map.put(k.toString(), list);
            } else if (v instanceof JSONObject) {
                // 如果内层是json对象的话,继续解析
                map.put(k.toString(), parseJSON2Map((JSONObject) v));
            } else {
                // 如果内层是普通对象的话,直接放入map中
                map.put(k.toString(), v);
            }
        }
        return map;
    }

    /**
     * 将json字符串转换为Map
     * @param jsonStr
     * @return
     */
    public static Map<String, Object> parseJSONstr2Map(String jsonStr) {
        JSONObject json = JSONObject.fromObject(jsonStr);
        Map<String, Object> map = parseJSON2Map(json);
        return map;
    }
}
 public static void main(String[] args) {

        //定义json对象
        JSONObject json = new JSONObject();
        json.put("id","1");
        json.put("name","张三");
        json.put("pwd","123456");
        System.out.println(json);


        //定义json字符串
        String json2 = "{\"country\":\"中国\",\"country_code_iso2\":\"CHN\",\"province\":\"北京市\"}";

        Map<String, Object> myresult = parseJSONstr2Map("{" +
                "\"data\": {" +
                "\"studentList\": [{" +
                "\"scourse\": {" +
                "\"cname\": \"语文,数学,英语\"," +
                "\"cscore\": \"93,95,98\"" +
                "}," +
                "\"sname\": \"张三\"," +
                "\"sage\": \"20\"," +
                "\"sid\": \"101\"" +
                "}," +
                "{" +
                "\"scourse\": {" +
                "\"cname\": \"物理,化学,生物\"," +
                "\"cscore\": \"92,93,97\"" +
                "}," +
                "\"sname\": \"李四\"," +
                "\"sage\": \"30\"," +
                "\"sid\": \"102\"" +
                "}]" +
                "}," +
                "\"resultCode\": \"1\"" +
                "}");



        System.out.println("json对象:"+json2+"\n");
        System.out.println("json对象-->map对象:"+parseJSONstr2Map(json2)+"\n");
        System.out.println("map对象-->json对象:"+JSONObject.fromObject(parseJSONstr2Map(json2)).toString()+"\n");
        //遍历简单map取出k,v对
        for(String key : parseJSONstr2Map(json2).keySet()){
            System.out.println("我是KEY:" + key);
            System.out.println("我是value:" + parseJSONstr2Map(json2).get(key));
        }
}
需要导入依赖:
        <dependency>
            <groupId>net.sf.json-lib</groupId>
            <artifactId>json-lib</artifactId>
            <version>2.4</version>
            <classifier>jdk15</classifier>
        </dependency>**加粗样式**
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值