需要下载第三方的jar :net.sf.json

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.collections.map.ListOrderedMap;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
/**
 * 
* 处理json的工具类.
* <br>本类为处理json的工具类
* @author slj
 */
public class JsonTools {

    /**
     * 
    * json转换list.
    * <br>详细说明
    * @param jsonStr json字符串
    * @return
    * @return List<Map<String,Object>> list
    * @throws
    * @author slj
    * @date 2013年12月24日 下午1:08:03
     */
    public static List<Map<String, Object>> parseJSON2List(String jsonStr){
        JSONArray jsonArr = JSONArray.fromObject(jsonStr);
        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()));
        }
        return list;
    }

   /**
    * 
   * json转换map.
   * <br>详细说明
   * @param jsonStr json字符串
   * @return
   * @return Map<String,Object> 集合
   * @throws
   * @author slj
    */
    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);
            }
        }
        return map;
    }

    /**
     * 
    * 通过HTTP获取JSON数据.
    * <br>通过HTTP获取JSON数据返回list
    * @param url 链接
    * @return
    * @return List<Map<String,Object>> list
    * @throws
    * @author slj
     */
    public static List<Map<String, Object>> getListByUrl(String url){
        try {
            //通过HTTP获取JSON数据
            InputStream in = new URL(url).openStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(in));
            StringBuilder sb = new StringBuilder();
            String line;
            while((line=reader.readLine())!=null){
                sb.append(line);
            }
            return parseJSON2List(sb.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

   /**
    * 
   * 通过HTTP获取JSON数据.
   * <br>通过HTTP获取JSON数据返回map
   * @param url 链接
   * @return
   * @return Map<String,Object> 集合
   * @throws
   * @author slj
    */
    public static Map<String, Object> getMapByUrl(String url){
        try {
            //通过HTTP获取JSON数据
            InputStream in = new URL(url).openStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(in));
            StringBuilder sb = new StringBuilder();
            String line;
            while((line=reader.readLine())!=null){
                sb.append(line);
            }
            return parseJSON2Map(sb.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    /**
     * 
    * map转换json.
    * <br>详细说明
    * @param map 集合
    * @return
    * @return String json字符串
    * @throws
    * @author slj
     */
    public static String mapToJson(Map<String, String> map) {
        Set<String> keys = map.keySet();
        String key = "";
        String value = "";
        StringBuffer jsonBuffer = new StringBuffer();
        jsonBuffer.append("{");
        for (Iterator<String> it = keys.iterator(); it.hasNext();) {
            key = (String) it.next();
            value = map.get(key);
            jsonBuffer.append(key + ":" +"\""+ value+"\"");
            if (it.hasNext()) {
                jsonBuffer.append(",");
            }
        }
        jsonBuffer.append("}");
        return jsonBuffer.toString();
    }

    //test
    public static void main(String[] args) {
        String url = "http://...";
        List<Map<String,Object>> list = getListByUrl(url);
        System.out.println(list);
    }
}

比如解析:

{"data":[{"id":"402880566092af74016092ba010f0026_151429025888228961","WI_ACTIVITY_ID_":"manuscripts","WI_ID_":"151429025888228961","workflowName":"志书编纂1","tableId":"8a7a543d5ccf1bb3015ccf412169006d","workflowId":"8a7aef3d5cd871bf015cd876c65b0000","workflowCode":"ZSBZ_NEW","PROCESS_INSTANCE_ID_":"151429025753518344","WI_STATUS_KEY_":"12","explain":"超级管理员申请的2017-12-26 20:09:12志书编纂1"},{"id":"83838a7a5fec0991015fec0b11d70010_151149367104810227","WI_ACTIVITY_ID_":"taskAssigned","WI_ID_":"151149367104810227","workflowName":"专题编研","tableId":"8a8aacd9564a3a1901564a882a6c0022","workflowId":"297ee40a56ca38590156caa6a0bb0008","workflowCode":"ZTBY_ZT","PROCESS_INSTANCE_ID_":"151149367010400495","WI_STATUS_KEY_":"12","explain":"超级管理员申请的2017-11-24 11:21:09专题编研"},{"id":"948181865e08e7b0015e091ea117011b_150339155625410132","WI_ACTIVITY_ID_":"activity2","WI_ID_":"150339155625410132","workflowName":"退回整理库","tableId":"402809815894218c01589560c7f90006","workflowId":"8383849458b8d25a0158b8dc9daa0000","workflowCode":"RETURNARRANGE","PROCESS_INSTANCE_ID_":"150339155603600640","WI_STATUS_KEY_":"12","explain":"超级管理员申请的2017-08-22 16:45:55退回整理库"}],"pageNumber":1,"pageSize":3,"total":3,"totalPages":1}
获取其中的data部分转换为List<Map<String,Object>>
    public List<Map<String,Object>> parseStringToListMap(String res) throws Exception{
        List<Map<String,Object>> list=new ArrayList<Map<String,Object>>();
        JSONObject jsonResult = new JSONObject(res);
        Iterator<String> nameItr = jsonResult.keys();
        while (nameItr.hasNext()) {
            String name = nameItr.next();
            if(name.equals("data")){
                String data=jsonResult.getString(name);
                JSONArray jsonArr = JSONArray.fromObject(data);
                for(int s=0;s<jsonArr.size();s++){//便利取到每个map
                    Map<String,Object> map = new HashMap<String,Object>();
                    String vas= jsonArr.get(s)==null?"":jsonArr.get(s).toString();
                    JSONObject jr=new JSONObject(vas);
                    Iterator<String> nir = jr.keys();
                    while (nir.hasNext()) {//取到map里的每个值
                        String json2 = nir.next();
                        String value= jr.getString(json2);
                        map.put(json2, value);
                    }
                    list.add(map);
                }
            }
        }
        return list;
    }

 










posted on 2018-05-09 12:46  车车12138  阅读(3516)  评论(0编辑  收藏  举报