package com.wtp.business.stathistory.util;

 

import java.io.IOException;

 

import java.util.ArrayList;

import java.util.HashSet;

import java.util.Iterator;

import java.util.List;

import java.util.Map;

import org.apache.commons.collections.map.ListOrderedMap;

import org.apache.commons.lang.Validate;

 

import net.sf.json.JSONArray;

import net.sf.json.JSONObject;

 

import com.wtp.frame.web.action.BaseAction;

 

/**

*

* 处理json的工具类. <br>

* 本类为处理json的工具类

*

* @author slj

*/

public class HistoricalUtil {

    public static List<Map<String, Object>> parseJSON2List(String json) {

        JSONArray jsonArr = JSONArray.fromObject(json);

        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

        Iterator<JSONObject> it = jsonArr.iterator();

        

        while (it.hasNext()) {

            JSONObject json2 = it.next();

            list.add(parseJSON2Map(json2.toString()));

        }

//         for (Map<String,Object> map:list

//                 ) {

//                for (String s:map.keySet()

//                     ) {

//                    System.out.print("key:"+s+"\t");

//                    System.out.println("value:"+map.get(s));

//                }

//            }

//        

        return list;

    }

 

    public static Map<String, Object> parseJSON2Map(String jsonStr) {

        ListOrderedMap map = new ListOrderedMap();

        // 最外层解析

        JSONObject json = JSONObject.fromObject(jsonStr);

 

        for (Object k : json.keySet()) {

            Object v = json.get(k);

            // 如果内层还是数组的话,继续解析

            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.toString()));

                }

                map.put(k.toString(), list);

            } else {

                map.put(k.toString(), v);

            }

        }

 

        Iterator iterator = map.keySet().iterator();

        List<String> lsList = new ArrayList<String>();

        int d=0;

 

        while (iterator.hasNext()) {

            

            String key = (String) iterator.next();

            Object object = map.get(key);    

            

            // 进行遍历删除,当值为空的时候删除这条数据

            if (object.equals("")) {

                iterator.remove();

                map.remove(key);

            }

            

        }

        return map;

    }

 

}

GitHub 加速计划 / js / json
41.72 K
6.61 K
下载
适用于现代 C++ 的 JSON。
最近提交(Master分支:1 个月前 )
960b763e 4 个月前
8c391e04 6 个月前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐