zoukankan      html  css  js  c++  java
  • 1、fastjson运用

    0、JSON使用阿里的 fastJson 为依赖包

    1、String转JSONObject

    JSONObject jSONObject = JSONObject.parseObject(String);

    2、String转JSONArray

    JSONArray jsonArray= JSONArray.parseArray(String);

    3、实例

    json串:

    {
        "data": {
            "route": {
                "destination": "109.56350818276404,39.575900296924715",
                "paths": [{
                        "distance": 200067,
                        "steps": [{
                                "action": "右转",
                                "assistant_action": "",
                                "cities": [{
                                        "adcode": "610802"
                                    }, {
                                        "adcode": "610802"
                                    }
                                ]
                            }, {
                                "action": "左转",
                                "assistant_action": "",
                                "cities": [{
                                        "adcode": "610802"
                                    }, {
                                        "adcode": "610802"
                                    }
                                ]
                            }
                        ],
                        "strategy": "时间最短"
                    }
                ]
            },
            "count": 1
        },
        "errmsg": "OK"
    }
    

      

    解析代码:

    import com.alibaba.fastjson.JSON;
    import com.alibaba.fastjson.JSONArray;
    import com.alibaba.fastjson.JSONObject;
    
    /**
     * @Date 2021/8/13 11:15
     * @Version 1.0
     */
    public class TestJsonParse {
        public static void main(String[] args) {
            String jsonStr = "{" +
                    "    "errmsg": "OK"," +
                    "    "data": {" +
                    "        "route": {" +
                    "            "destination": "109.56350818276404,39.575900296924715"," +
                    "            "paths": [{" +
                    "                    "distance": 200067," +
                    "                    "steps": [{" +
                    "                            "action": "右转"," +
                    "                            "assistant_action": ""," +
                    "                            "cities": [{" +
                    "                                    "adcode": "610802"" +
                    "                                }, {" +
                    "                                    "adcode": "610802"" +
                    "                                }" +
                    "                            ]" +
                    "                        }, {" +
                    "                            "action": "左转"," +
                    "                            "assistant_action": ""," +
                    "                            "cities": [{" +
                    "                                    "adcode": "610802"" +
                    "                                }, {" +
                    "                                    "adcode": "610802"" +
                    "                                }" +
                    "                            ]" +
                    "                        }" +
                    "                    ]," +
                    "                    "strategy": "时间最短"" +
                    "                }" +
                    "            ]" +
                    "        }," +
                    "        "count": 1" +
                    "    }" +
                    "}";
    
            // 第一层
            JSONObject one = JSONObject.parseObject(jsonStr);
            System.out.println("data: " + one.get("errmsg").toString());
            // 第二层
            JSONObject two = JSON.parseObject( one.get("data").toString());
            // 第三层
            JSONObject three = JSON.parseObject( two.get("route").toString());
            // 第四层 数组
            JSONArray four = JSON.parseArray( three.get("paths").toString());
            // 第五层 获取数组中,第一个对象
            JSONObject five = four.getJSONObject(0);
            System.out.println("distance: " + five.get("distance"));
        }
    }
    
  • 相关阅读:
    微信公众平台申请消息接口验证工具
    Android应用开发学习之启动另外一个Activity
    九宫格数值分组
    Squid--hash代码分析
    ThreadPoolExecutor原理和使用
    [数字dp] hdu 3271 SNIBB
    C#同步SQL Server数据库Schema
    [AC自己主动机] zoj Searching the String
    人活着系列Tanya和蔡健雅猪 (floyd)
    安装在谷歌axure小工具
  • 原文地址:https://www.cnblogs.com/651434092qq/p/15138178.html
Copyright © 2011-2022 走看看