2021-05-22

解析json字符串

package org.example;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
public class Json {
 public static void main(String[] args) {
        //json格式转字符串

        String data ="{\"code\":200,\"msg\":\"ok\",\"data\":[{\"ip\":\"121.56.213.160\",\"port\":36410,\"expire_time\":\"2021-05-08 15:45:26\",\"city\":\"\\u9521\\u6797\\u90ed\\u52d2\\u76df\",\"isp\":\"\\u7535\\u4fe1\"},{\"ip\":\"114.227.161.205\",\"port\":23564,\"expire_time\":\"2021-05-08 15:55:40\",\"city\":\"\\u5e38\\u5dde\",\"isp\":\"\\u7535\\u4fe1\"},{\"ip\":\"117.91.252.183\",\"port\":5412,\"expire_time\":\"2021-05-08 15:53:29\",\"city\":\"\\u626c\\u5dde\",\"isp\":\"\\u7535\\u4fe1\"},{\"ip\":\"114.100.1.151\",\"port\":766,\"expire_time\":\"2021-05-08 15:50:34\",\"city\":\"\\u9a6c\\u978d\\u5c71\",\"isp\":\"\\u7535\\u4fe1\"},{\"ip\":\"60.166.102.108\",\"port\":5412,\"expire_time\":\"2021-05-08 15:35:37\",\"city\":\"\\u5408\\u80a5\",\"isp\":\"\\u7535\\u4fe1\"},{\"ip\":\"49.87.194.11\",\"port\":36410,\"expire_time\":\"2021-05-08 15:48:42\",\"city\":\"\\u6dee\\u5b89\",\"isp\":\"\\u7535\\u4fe1\"},{\"ip\":\"113.110.45.234\",\"port\":36410,\"expire_time\":\"2021-05-08 15:40:46\",\"city\":\"\\u63ed\\u9633\",\"isp\":\"\\u7535\\u4fe1\"},{\"ip\":\"49.86.179.128\",\"port\":5412,\"expire_time\":\"2021-05-08 15:40:56\",\"city\":\"\\u626c\\u5dde\",\"isp\":\"\\u7535\\u4fe1\"},{\"ip\":\"117.60.239.175\",\"port\":3617,\"expire_time\":\"2021-05-08 15:58:12\",\"city\":\"\\u6dee\\u5b89\",\"isp\":\"\\u7535\\u4fe1\"},{\"ip\":\"121.56.213.229\",\"port\":5412,\"expire_time\":\"2021-05-08 15:40:33\",\"city\":\"\\u9521\\u6797\\u90ed\\u52d2\\u76df\",\"isp\":\"\\u7535\\u4fe1\"},{\"ip\":\"113.231.37.204\",\"port\":5412,\"expire_time\":\"2021-05-08 15:45:21\",\"city\":\"\\u8fbd\\u9633\",\"isp\":\"\\u8054\\u901a\"},{\"ip\":\"121.56.215.168\",\"port\":894,\"expire_time\":\"2021-05-08 15:50:26\",\"city\":\"\\u9521\\u6797\\u90ed\\u52d2\\u76df\",\"isp\":\"\\u7535\\u4fe1\"},{\"ip\":\"49.87.201.5\",\"port\":3617,\"expire_time\":\"2021-05-08 15:35:16\",\"city\":\"\\u6dee\\u5b89\",\"isp\":\"\\u7535\\u4fe1\"},{\"ip\":\"36.56.101.254\",\"port\":894,\"expire_time\":\"2021-05-08 15:50:53\",\"city\":\"\\u9a6c\\u978d\\u5c71\",\"isp\":\"\\u7535\\u4fe1\"},{\"ip\":\"125.105.176.171\",\"port\":766,\"expire_time\":\"2021-05-08 15:52:56\",\"city\":\"\\u8862\\u5dde\",\"isp\":\"\\u7535\\u4fe1\"},{\"ip\":\"114.97.194.216\",\"port\":23564,\"expire_time\":\"2021-05-08 15:35:53\",\"city\":\"\\u5408\\u80a5\",\"isp\":\"\\u7535\\u4fe1\"},{\"ip\":\"42.56.34.65\",\"port\":766,\"expire_time\":\"2021-05-08 15:40:55\",\"city\":\"\\u8fbd\\u9633\",\"isp\":\"\\u8054\\u901a\"},{\"ip\":\"114.100.2.128\",\"port\":23564,\"expire_time\":\"2021-05-08 15:50:21\",\"city\":\"\\u9a6c\\u978d\\u5c71\",\"isp\":\"\\u7535\\u4fe1\"},{\"ip\":\"60.166.104.104\",\"port\":5412,\"expire_time\":\"2021-05-08 15:40:17\",\"city\":\"\\u5408\\u80a5\",\"isp\":\"\\u7535\\u4fe1\"},{\"ip\":\"60.166.103.174\",\"port\":5412,\"expire_time\":\"2021-05-08 15:40:29\",\"city\":\"\\u5408\\u80a5\",\"isp\":\"\\u7535\\u4fe1\"}]}";
        //解析json字符串
        //1.借助外部json工具ali fastjson

        /**
         * com.alibaba.fastjson.JSONObject
         * com.alibaba.fastjson.JSONArray
         */

        JSONObject json = JSON.parseObject(data);
        System.out.println(json.getIntValue("code"));
        System.out.println(json.getString("msg"));

        System.out.println("========================================");
        JSONArray jarr = json.getJSONArray("data");

       for(int i=0;i<jarr.size();i++){
           json = jarr.getJSONObject(i);
           System.out.println(json.getString("ip"));
           System.out.println(json.getIntValue("port"));
           System.out.println(json.getString("expire_time"));
           System.out.println(json.getString("city") );
           System.out.println(json.getString("isp"));

    	  }
  	 }
 }