zoukankan      html  css  js  c++  java
  • Java基础学习总结(54)——JSON和Map转换的工具类

    在pom.xml文件中引入如下jar

    <dependency>

    <groupId>commons-lang</groupId>
    <artifactId>commons-lang</artifactId>
    <version>2.6</version>
    </dependency>
    <dependency>
    <groupId>commons-beanutils</groupId>
    <artifactId>commons-beanutils</artifactId>
    <version>1.8.0</version>
    </dependency>
    <dependency>
    <groupId>commons-collections</groupId>
    <artifactId>commons-collections</artifactId>
    <version>3.2.1</version>
    </dependency>
    <dependency>
    <groupId>commons-logging</groupId>
    <artifactId>commons-logging</artifactId>
    <version>1.1.1</version>
    </dependency>
    <!--json-lib -->
    <dependency>
    <groupId>net.sf.json-lib</groupId>
    <artifactId>json-lib</artifactId>
    <version>2.4</version>
    <classifier>jdk15</classifier>

    </dependency>

    下载下面的jar放入仓库相应的位置:D:javamavenmaven_repo etsfjson-libjson-lib2.4

    具体代码如下:

    package com.zhy.robot.util;


    import java.io.BufferedReader;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.net.URL;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.List;
    import java.util.Map;


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


    /**
     * @ClassName: JsonMapUtil
     * @Description: TODO(这里用一句话描述这个类的作用)
     * @author Administrator
     * @date 2016年4月27日 上午10:19:11
     * @Copyright © 2016上海通善互联网金融信息服务有限公司
     */
    public class JsonMapUtil {

    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;
    }

    public static Map<String, Object> parseJSON2Map(String jsonStr) {
    Map<String, Object> map = new HashMap<String, Object>();
    // 最外层解析
    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;
    }

    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;
    }

    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;
    }

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



  • 相关阅读:
    密码系统的安全性
    liboqs-量子安全密码算法开源C库
    密码基础知识(1)
    同态加密
    NTL 库函数
    以太坊相关的区块链知识
    53字符流中第一个不重复的字符
    基础小知识总结
    52表示数值的字符串 记忆
    HIHOcoder编程总结
  • 原文地址:https://www.cnblogs.com/zhanghaiyang/p/7213139.html
Copyright © 2011-2022 走看看