Java获取并解析服务器端的JSON数据包

本文介绍了如何在Spring Boot项目中使用Java从微信API获取并解析JSON数据,创建了一个JSONAnalysis静态工具类。通过引入JSON依赖,可以将接收到的数据转换为Java对象。文中提供了JSON API的测试接口和数据源,帮助理解并实践JSON的解析过程。
摘要由CSDN通过智能技术生成

最近做spring boot项目,需要从微信api获取用户数据,并加以解析,转换成Java中的对象,所以就决定写一个JSONAnalysis静态工具类,在需要的时候只需调用该方法即可。

参考资料: JSONObject,JSONArray,Map,String之间转换

这个网址里有很多JSON API免费接口

http://www.bejson.com/knownjson/webInterface/

 JSON对象测试数据源

http://www.kuaidi100.com/query?type=yuantong&postid=11111111111

以上两个网址接口数据仅作为本次学习的测试数据

1)在pom文件里加入json依赖,或者下载相应的json的jar包,载入classpath路径

<dependency>
    <groupId>net.sf.json-lib</groupId>
    <artifactId>json-lib</artifactId>
    <version>2.4</version>
    <classifier>jdk15</classifier>
</dependency>

 

2)JSONAnalysis静态工具类

package JSON;

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

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.Map;



public class JSONAnalysis {
    /**
     * 根据url获得json字符串
     * @param url
     * @return
     */
    public static String loadJson(String url)  {
        StringBuilder json = new StringBuilder();
        try {

            URL urlObject = new URL(url);
            URLConnection uc  = urlObject.openConnection();
            BufferedReader in = new BufferedReader(new InputStreamReader(uc.getInputStream()));
            String inputLine = null;
            while((inputLine = in.readLine())!=null){
                json.append(inputLine);
            }
            in.close();
        }catch (Exception e) {
            e.printStackTrace();
        }
        return json.toString();
    }

    /**
     * 处理JSON对象数据据包,以<键,值>对的形式存入java.util.Map
     * @param String
     * @return java.util.Map
     */
    public static Map<String, Object> getKeytoValueFromJsonObject(String jsonStr){
        System.out.println("---解析JSON对象数据包---");

        //将json字符串转化为JSONObject
        JSONObject jsonObject = JSONObject.fromObject(jsonStr);

        Map<String, Object> map = jsonObject;

        return map;
    }
    /**
     * 处理JSON数组对象数据据包,以<键,值>对的形式存入java.util.Map
     * @param String
     * @return java.util.Map[]
     */
    public static Map<String, Object>[] getKeytoValueFromJsonArray(String jsonStr){
        System.out.println("---解析JSON数组对象数据包---");
        //将json字符串转化为JSONObject
        JSONArray jsonArray = JSONArray.fromObject(jsonStr);
        Map<String, Object>[] mapArray  = new Map[jsonArray.size()];
        for(int i=0 ; i < jsonArray.size(); i++) {
            //获取每一个JsonObject对象
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            mapArray[i] = jsonObject;
        }
        return mapArray;
    }


    public static void main(String[] args) {
        String jsonData = loadJson("http://www.kuaidi100.com/query?type=yuantong&postid=11111111111");
        System.out.println(jsonData);

        Map<String, Object> map = getKeytoValueFromJsonObject(jsonData);
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
        }

        //map.get("data")将获得一个JSONArray对象
        String jsonArr = map.get("data").toString();
        System.out.println(jsonArr);

        Map<String, Object>[] mapArr = getKeytoValueFromJsonArray(jsonArr); int i = 1;
        for(Map<String, Object> mapObj : mapArr) {
            System.out.println("mapArr#" + i++ + "#" );
            for (Map.Entry<String, Object> entry : mapObj.entrySet()) {
                System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
            }

            System.out.println();
        }

        System.out.println("It's OK!");
    }
}

 3)编译并运行,程序情况如下:

"C:\Program Files\Java\jdk1.8.0_131\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2018.2.1\lib\idea_rt.jar=60594:C:\Program Files\JetBrains\IntelliJ IDEA 2018.2.1\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_131\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\rt.jar;D:\IdeaProjects\JavaStudy\target\classes;E:\maven\repository\net\sf\json-lib\json-lib\2.4\json-lib-2.4-jdk15.jar;E:\maven\repository\commons-beanutils\commons-beanutils\1.8.0\commons-beanutils-1.8.0.jar;E:\maven\repository\commons-collections\commons-collections\3.2.1\commons-collections-3.2.1.jar;E:\maven\repository\commons-lang\commons-lang\2.5\commons-lang-2.5.jar;E:\maven\repository\commons-logging\commons-logging\1.1.1\commons-logging-1.1.1.jar;E:\maven\repository\net\sf\ezmorph\ezmorph\1.0.6\ezmorph-1.0.6.jar" JSON.JSONAnalysis
{"message":"ok","nu":"11111111111","ischeck":"0","condition":"00","com":"yuantong","status":"200","state":"5","data":[{"time":"2018-08-11 21:21:02","ftime":"2018-08-11 21:21:02","context":"明怡花园14栋5号车库妈妈驿站已发出自提短信,请上门自提,联系电话18105715870","location":null},{"time":"2018-08-11 21:21:02","ftime":"2018-08-11 21:21:02","context":"快件已到达明怡花园14栋5号车库妈妈驿站,联系电话18105715870","location":null},{"time":"2018-08-09 09:25:34","ftime":"2018-08-09 09:25:34","context":"临江丽苑3期A区8栋圆通速递妈妈驿站已发出自提短信,请上门自提,联系电话17345883730","location":null},{"time":"2018-08-09 09:24:34","ftime":"2018-08-09 09:24:34","context":"快件已到达临江丽苑3期A区8栋圆通速递妈妈驿站,联系电话17345883730","location":null},{"time":"2018-08-04 09:56:26","ftime":"2018-08-04 09:56:26","context":"中南本部五食堂对面圆通速递妈妈驿站已发出自提短信,请上门自提,联系电话18973127225","location":null},{"time":"2018-08-04 09:55:26","ftime":"2018-08-04 09:55:26","context":"快件已到达中南本部五食堂对面圆通速递妈妈驿站,联系电话18973127225","location":null},{"time":"2018-08-01 16:18:30","ftime":"2018-08-01 16:18:30","context":"大坪乡农商银行旁边肖五生家妈妈驿站已发出自提短信,请上门自提,联系电话15079736558","location":null},{"time":"2018-08-01 16:17:30","ftime":"2018-08-01 16:17:30","context":"快件已到达大坪乡农商银行旁边肖五生家妈妈驿站,联系电话15079736558","location":null},{"time":"2018-06-29 11:33:22","ftime":"2018-06-29 11:33:22","context":"中睿城1号楼门卫旁妈妈驿站妈妈驿站已发出自提短信,请上门自提,联系电话18659973879","location":null},{"time":"2018-06-29 11:33:22","ftime":"2018-06-29 11:33:22","context":"快件已到达中睿城1号楼门卫旁妈妈驿站妈妈驿站,联系电话18659973879","location":null},{"time":"2018-06-26 09:18:53","ftime":"2018-06-26 09:18:53","context":"阳湖八一大道2-28号圆通快递妈妈驿站已发出自提短信,请上门自提,联系电话13855976229","location":null},{"time":"2018-06-26 09:18:53","ftime":"2018-06-26 09:18:53","context":"快件已到达阳湖八一大道2-28号圆通快递妈妈驿站,联系电话13855976229","location":null},{"time":"2018-06-17 14:43:52","ftime":"2018-06-17 14:43:52","context":"快件已到达快递到了小吃街茁耳零食店了妈妈驿站,联系电话13662115693","location":null},{"time":"2018-06-17 14:43:52","ftime":"2018-06-17 14:43:52","context":"快递到了小吃街茁耳零食店了妈妈驿站已发出自提短信,请上门自提,联系电话13662115693","location":null},{"time":"2018-06-10 12:21:24","ftime":"2018-06-10 12:21:24","context":"杨厂村正街路南圆通代办点妈妈驿站已发出自提短信,请上门自提,联系电话13849372377","location":null},{"time":"2018-06-10 12:21:24","ftime":"2018-06-10 12:21:24","context":"快件已到达杨厂村正街路南圆通代办点妈妈驿站,联系电话13849372377","location":null},{"time":"2018-06-07 16:08:58","ftime":"2018-06-07 16:08:58","context":"快件已到达亭知路672号业务员正在派送中妈妈驿站,联系电话021-67879206","location":null},{"time":"2018-06-07 16:08:58","ftime":"2018-06-07 16:08:58","context":"亭知路672号业务员正在派送中妈妈驿站已发出自提短信,请上门自提,联系电话021-67879206","location":null},{"time":"2018-05-31 20:07:02","ftime":"2018-05-31 20:07:02","context":"快件已到达临沂北路技师学院圆通快递超市妈妈驿站,联系电话18106332770","location":null},{"time":"2018-05-31 20:07:02","ftime":"2018-05-31 20:07:02","context":"临沂北路技师学院圆通快递超市妈妈驿站已发出自提短信,请上门自提,联系电话18106332770","location":null},{"time":"2018-05-30 10:57:46","ftime":"2018-05-30 10:57:46","context":"快件已到达泽州路98号拿快递妈妈驿站妈妈驿站,联系电话18221459909","location":null},{"time":"2018-05-30 10:57:46","ftime":"2018-05-30 10:57:46","context":"泽州路98号拿快递妈妈驿站妈妈驿站已发出自提短信,请上门自提,联系电话18221459909","location":null},{"time":"2018-05-29 19:53:25","ftime":"2018-05-29 19:53:25","context":"快件已到达西校园菜鸟驿站即将发送提货码妈妈驿站,联系电话18577846400","location":null},{"time":"2018-05-25 09:44:25","ftime":"2018-05-25 09:44:25","context":"佳源花都大门口圆通店妈妈驿站已发出自提短信,请上门自提,联系电话15377667309","location":null},{"time":"2018-05-25 09:44:24","ftime":"2018-05-25 09:44:24","context":"快件已到达佳源花都大门口圆通店妈妈驿站,联系电话15377667309","location":null},{"time":"2018-05-23 12:16:35","ftime":"2018-05-23 12:16:35","context":"快递到新开的快递超市芙蓉超市旁妈妈驿站已发出自提短信,请上门自提,联系电话17723937369","location":null},{"time":"2018-05-23 12:16:32","ftime":"2018-05-23 12:16:32","context":"快件已到达快递到新开的快递超市芙蓉超市旁妈妈驿站,联系电话17723937369","location":null},{"time":"2018-05-09 11:07:09","ftime":"2018-05-09 11:07:09","context":"快件已到达火炬二路纬泰超市圆通妈妈驿站妈妈驿站,联系电话18870011859","location":null},{"time":"2018-05-09 11:07:09","ftime":"2018-05-09 11:07:09","context":"火炬二路纬泰超市圆通妈妈驿站妈妈驿站已发出自提短信,请上门自提,联系电话18870011859","location":null},{"time":"2018-05-06 10:13:51","ftime":"2018-05-06 10:13:51","context":"东坡小区幼儿园南碧水5号楼圆通妈妈驿站已发出自提短信,请上门自提,联系电话15966106271","location":null},{"time":"2018-05-06 10:13:51","ftime":"2018-05-06 10:13:51","context":"快件已到达东坡小区幼儿园南碧水5号楼圆通妈妈驿站,联系电话15966106271","location":null},{"time":"2018-04-25 13:25:07","ftime":"2018-04-25 13:25:07","context":"快递请到锦尚城14栋1-7取件妈妈驿站已发出自提短信,请上门自提,联系电话17318411160","location":null},{"time":"2018-04-25 13:25:07","ftime":"2018-04-25 13:25:07","context":"快件已到达快递请到锦尚城14栋1-7取件妈妈驿站,联系电话17318411160","location":null},{"time":"2018-04-23 09:14:53","ftime":"2018-04-23 09:14:53","context":"快件已到达东门请17点及时取件圆通快递妈妈驿站,联系电话18168989760","location":null},{"time":"2018-04-23 09:14:53","ftime":"2018-04-23 09:14:53","context":"东门请17点及时取件圆通快递妈妈驿站已发出自提短信,请上门自提,联系电话18168989760","location":null},{"time":"2018-04-08 14:55:39","ftime":"2018-04-08 14:55:39","context":"警苑小区院内1号楼西侧33车库妈妈驿站已发出自提短信,请上门自提,联系电话13123712883","location":null},{"time":"2018-04-08 14:55:39","ftime":"2018-04-08 14:55:39","context":"快件已到达警苑小区院内1号楼西侧33车库妈妈驿站,联系电话13123712883","location":null},{"time":"2018-04-07 14:58:29","ftime":"2018-04-07 14:58:29","context":"兴盛园小北门妈妈驿站妈妈驿站已发出自提短信,请上门自提,联系电话18192082506","location":null},{"time":"2018-04-07 14:58:29","ftime":"2018-04-07 14:58:29","context":"快件已到达兴盛园小北门妈妈驿站妈妈驿站,联系电话18192082506","location":null},{"time":"2018-04-05 09:11:53","ftime":"2018-04-05 09:11:53","context":"乐尚二楼超市入口对面17点后取妈妈驿站已发出自提短信,请上门自提,联系电话18918021456","location":null},{"time":"2018-04-05 09:11:53","ftime":"2018-04-05 09:11:53","context":"快件已到达乐尚二楼超市入口对面17点后取妈妈驿站,联系电话18918021456","location":null},{"time":"2018-03-13 11:30:29","ftime":"2018-03-13 11:30:29","context":"快件已到达黎托街道黎锦苑8栋106号妈妈驿站,如有疑问请联系13755027160","location":null},{"time":"2018-03-13 11:30:29","ftime":"2018-03-13 11:30:29","context":"黎托街道黎锦苑8栋106号妈妈驿站已发出自提短信,请上门自提,如有疑问请联系13755027160","location":null},{"time":"2018-03-10 20:16:41","ftime":"2018-03-10 20:16:41","context":"国际关系学院妈妈驿站已发出自提短信,请上门自提,如有疑问请联系17610012989","location":null},{"time":"2018-03-10 20:16:41","ftime":"2018-03-10 20:16:41","context":"快件已到达国际关系学院妈妈驿站,如有疑问请联系17610012989","location":null},{"time":"2018-03-09 12:25:35","ftime":"2018-03-09 12:25:35","context":"孙伯镇红绿灯北800米路东圆通妈妈驿站已发出自提短信,请上门自提,如有疑问请联系18105389267","location":null},{"time":"2018-03-09 12:25:35","ftime":"2018-03-09 12:25:35","context":"快件已到达孙伯镇红绿灯北800米路东圆通妈妈驿站,如有疑问
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值