package com.dgdr.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.serializer.SerializerFeature;

/**
 * json操作工具类
 * 
 * @author yangtao
 *
 */
public final class JSONUtil {

	/**
	 * 默认json格式化方式
	 */
	public static final SerializerFeature[] DEFAULT_FORMAT = { SerializerFeature.WriteDateUseDateFormat,
			SerializerFeature.WriteEnumUsingToString, SerializerFeature.WriteNonStringKeyAsString,
			SerializerFeature.QuoteFieldNames, SerializerFeature.SkipTransientField, SerializerFeature.SortField,
			SerializerFeature.PrettyFormat };

	private JSONUtil() {
	}

	/**
	 * 用自定义模板从json字符串中取值
	 * 
	 * @param json
	 * @param template demo:items.items.x_item.0[].open_iid---->0[]:[]表示数组,0表示取第几个
	 * @return
	 * @author yangtao
	 */
	public static String getJSONValueByTemplate(String json, String template) {
		String keys[] = template.split("\\.");
		for (String key : keys) {
			if (json == null) {
				return null;
			}
			if (key.indexOf("[]") > 0) {
				json = JSON.parseObject(json, new TypeReference<List<String>>(){}).get(Integer.parseInt(key.replace("[]", "")));
			} else{
				json = JSON.parseObject(json).getString(key);
			}
		}
		return json;
	}
	
	/**
	 * JSON转Map
	 * @param jsonStr
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public static Map<String, Object> parseJSON2Map(String jsonStr){ 
        Map<String, Object> map = new HashMap<String, Object>();  
        //最外层解析  
        JSONObject json = JSONObject.parseObject(jsonStr);  
        for(Object k : json.keySet()){ 
            Object v = json.get(k);   
            //如果内层还是数组的话,继续解析  
            if(v instanceof JSONArray){
            	//数组对象又分为两种(简单数组或键值对)
                List list = parseJSON2List(json.get(k).toString());
				map.put((String)k, list);  
            } else {  
                map.put((String)k, v);  
            }  
        }  
        return map;  
    }
	
	/**
	 * JSON转List<Map<String, Object>>
	 * @param json
	 */
    @SuppressWarnings({ "rawtypes", "unchecked" })
	public static List<Map<String, Object>> parseJSON2List(String json){
    	JSONArray jsonArray = JSONArray.parseArray(json);
        List list = new ArrayList();
        for (Object object : jsonArray) {
        	try {
        		JSONObject jsonObject = (JSONObject) object;
                HashMap<String, Object> map = new HashMap<String, Object>();
                for (Map.Entry entry : jsonObject.entrySet()) {
                    if(entry.getValue() instanceof  JSONArray){
                        map.put((String)entry.getKey(), parseJSON2List(entry.getValue().toString()));
                    }else{
                        map.put((String)entry.getKey(), entry.getValue());
                    }
                }
                list.add(map);
			} catch (Exception e) {
				list.add(object);
			}
        }
        return list;
    }  
}
最近下载更多
503382513  LV10 2022年1月19日
digbig  LV1 2021年12月23日
chokkint  LV12 2021年10月26日
1358849392  LV21 2020年12月31日
guo0021  LV1 2020年5月18日
kenpfang  LV18 2019年10月7日
289345424  LV1 2019年9月16日
0312wangchen  LV26 2019年9月12日
christzhsh  LV1 2019年9月4日
2224947710  LV17 2018年8月18日
最近浏览更多
你爹正在加载中  LV4 2023年7月18日
1234mama  LV19 2022年4月13日
503382513  LV10 2022年1月19日
digbig  LV1 2021年12月23日
358218675 2021年11月10日
暂无贡献等级
chokkint  LV12 2021年10月26日
ewan007  LV29 2021年10月10日
百里守寡  LV5 2021年4月2日
Myangyyyy  LV10 2021年3月11日
tonyhawkjm  LV22 2021年1月19日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友