关键词搜索

源码搜索 ×
×

不同格式的json解析

发布2021-10-24浏览764次

详情内容

一、基本数据格式

给服务端发送请求后,服务端会返回一连串的数据,这些数据在大部分情况下都是XML格式或者JSON格式。然后JSON相对XML来说解析相对方便一些,所以先说说JSON的解析。

JSON的基本数据格式有这几种:

1. 一个JSON对象

JSONObject

{
    "name": "胡小威",
    "age": 20,
    "male": true
}
    2.一个JSON数组

    JSONArray

    
    [
        {
            "name": "胡小威",
            "age": 20,
            "male": true
        },
        {
            "name": "赵小亮",
            "age": 22,
            "male": false
        }
    ]
    
      6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    3.复杂JSONObject
    
    {
        "name": "胡小威",
        "age": 20,
        "male": true,
        "address": {
            "street": "岳麓山南",
            "city": "长沙",
            "country": "中国"
        }
    }
    
      6
    • 7
    • 8
    • 9
    • 10
    • 11
    4. 复杂JSONArray
    [
        {
            "name": "胡小威",
            "age": 20,
            "male": true,
            "address": {
                "street": "岳麓山南",
                "city": "长沙",
                "country": "中国"
            }
        },
        {
            "name": "赵小亮",
            "age": 22,
            "male": false,
            "address": {
                "street": "九州港",
                "city": "珠海",
                "country": "中国"
            }
        }
    ]
    
      6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    5.更复杂的json

    爱帮公交上扒取的数据

    {
        "buses": {
            "bus": [
                {
                    "last_foot_dist": "0",
                    "time": "37",
                    "segments": {
                        "segment": [
                            {
                                "line_name": "立珊专线(中南大学学生公寓-长沙火车站)",
                                "foot_dist": "362",
                                "stat_xys": "",
                                "stats": "岳麓山南;湖南师大;二里半;岳麓山北;市四医院;华图教育(太平街口);牛耳教育(南阳街口);韭菜园;曙光路口;长岛路口;长沙火车站",
                                "end_stat": "长沙火车站",
                                "line_xys": "",
                                "line_dist": "7535",
                                "start_stat": "岳麓山南"
                            }
                        ]
                    },
                    "foot_dist": "362",
                    "dist": "7897"
                },
                {
                    "last_foot_dist": "0",
                    "time": "41",
                    "segments": {
                        "segment": [
                            {
                                "line_name": "旅1路(科教新村-长沙火车站)",
                                "foot_dist": "337",
                                "stat_xys": "",
                                "stats": "岳麓山南;湖南师大;二里半;岳麓山北;市四医院;华图教育(太平街口);蔡锷中路口;韭菜园;乔庄;曙光路口;长岛路口;五一东村;车站路口;长沙火车站",
                                "end_stat": "长沙火车站",
                                "line_xys": "",
                                "line_dist": "7822",
                                "start_stat": "岳麓山南"
                            }
                        ]
                    },
                    "foot_dist": "337",
                    "dist": "8159"
                }
            ]
        }
    }
    
      6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    二、解析
    2.1. JSONObject解析
    {
        "name": "胡小威",
        "age": 20,
        "male": true
    }
    
      package com.gblfy;
      
      import com.alibaba.fastjson.JSON;
      import com.alibaba.fastjson.JSONArray;
      import com.alibaba.fastjson.JSONException;
      import com.alibaba.fastjson.JSONObject;
      import com.gblfy.utils.FastJsonUtil;
      import org.junit.Test;
      
      import java.util.List;
      
      public class JsonApplicationTests {
      
          //把JSON数据转换成指定的java对象
          @Test
          public void fastJsonTest() {
              Student st = new Student().builder()
                      .name("雨昕")
                      .age(2)
                      .build();
      
              String json = FastJsonUtil.getBeanToJson(st);
              System.out.println("********对象转Json*********");
              System.out.println("json ::" + json);
              System.out.println("********Json转对象*********");
              Student newst = FastJsonUtil.getJsonToBean(json, Student.class);
              System.out.println("Name ::" + newst.getName());
              System.out.println("Age ::" + newst.getAge());
          }
      
      
          //解析JsonObject
          @Test
          public void parseJsonObject() {
              try {
                  String json = "{\n" +
                          "    \"name\": \"胡小威\",\n" +
                          "    \"age\": 20,\n" +
                          "    \"male\": true\n" +
                          "}";
      
                  System.out.println("*********单纯的从json数据中获取对应的值*********");
                  JSONObject jb = JSON.parseObject(json);
                  String name = jb.getString("name");
                  int age = jb.getInteger("age");
                  boolean male = jb.getBoolean("male");
      
                  System.out.println("name:" + name);
                  System.out.println("age:" + age);
                  System.out.println("male:" + male);
      
                  System.out.println("*********把JSON数据转换成指定的java对象*********");
                  Student st = FastJsonUtil.getJsonToBean(json, Student.class);
                  System.out.println("*********Student*********");
      
                  System.out.println("name:" + st.getName());
                  System.out.println("age:" + st.getAge());
                  System.out.println("male:" + st.isMale());
      
                  //接下来该干嘛干嘛
              } catch (JSONException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
          }
      
          //解析JSONArray
          @Test
          public void parseJSONArray() {
              String json = "[\n" +
                      "    {\n" +
                      "        \"name\": \"胡小威\",\n" +
                      "        \"age\": 20,\n" +
                      "        \"male\": true\n" +
                      "    },\n" +
                      "    {\n" +
                      "        \"name\": \"赵小亮\",\n" +
                      "        \"age\": 22,\n" +
                      "        \"male\": false\n" +
                      "    }\n" +
                      "]";
              try {
      
                  System.out.println("*********把JSON数据转换成指定的java对象列表*********");
                  List<Student> jsonToList = FastJsonUtil.getJsonToList(json, Student.class);
                  for (Student st : jsonToList) {
                      System.out.println("name: " + st.getName());
                      System.out.println("age: " + st.getAge());
                      System.out.println("male: " + st.isMale());
                  }
      
                  System.out.println("*********解析JSONArray*********");
                  JSONArray jsonArray = JSON.parseArray(json);
                  for (Object o : jsonArray) {
                      Student st = (Student) o;
                      System.out.println("name: " + st.getName());
                      System.out.println("age: " + st.getAge());
                      System.out.println("male: " + st.isMale());
                  }
      
                  //接下来该干嘛干嘛
              } catch (JSONException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
          }
      
          //解析复杂JSONObject
          @Test
          public void parseJsonPerson() {
              String json = "{\n" +
                      "    \"name\": \"胡小威\",\n" +
                      "    \"age\": 20,\n" +
                      "    \"male\": true,\n" +
                      "    \"address\": {\n" +
                      "        \"street\": \"岳麓山南\",\n" +
                      "        \"city\": \"长沙\",\n" +
                      "        \"country\": \"中国\"\n" +
                      "    }\n" +
                      "}";
              try {
                  JSONObject jb = JSON.parseObject(json);
                  String name = jb.getString("name");
                  int age = jb.getInteger("age");
                  boolean male = jb.getBoolean("male");
      
                  JSONObject addressJSON = jb.getJSONObject("address");
                  String street = addressJSON.getString("street");
                  String city = addressJSON.getString("city");
                  String country = addressJSON.getString("country");
      
                  Address address = new Address(street, city, country);
                  Student student = new Student(name, age, male, address);
      
                  System.out.println("name: " + name);
                  System.out.println("age: " + age);
                  System.out.println("male: " + male);
      
                  System.out.println("street: " + street);
                  System.out.println("city: " + city);
                  System.out.println("country: " + country);
      
              } catch (JSONException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
          }
      
          //解析复杂JSONArray
          @Test
          public void parseJsonArrayPerson() {
              String json="[\n" +
                      "    {\n" +
                      "        \"name\": \"胡小威\",\n" +
                      "        \"age\": 20,\n" +
                      "        \"male\": true,\n" +
                      "        \"address\": {\n" +
                      "            \"street\": \"岳麓山南\",\n" +
                      "            \"city\": \"长沙\",\n" +
                      "            \"country\": \"中国\"\n" +
                      "        }\n" +
                      "    },\n" +
                      "    {\n" +
                      "        \"name\": \"赵小亮\",\n" +
                      "        \"age\": 22,\n" +
                      "        \"male\": false,\n" +
                      "        \"address\": {\n" +
                      "            \"street\": \"九州港\",\n" +
                      "            \"city\": \"珠海\",\n" +
                      "            \"country\": \"中国\"\n" +
                      "        }\n" +
                      "    }\n" +
                      "]";
              try {
                  System.out.println("*********把JSON数据转换成指定的java对象列表*********");
                  List<Student> jsonToList = FastJsonUtil.getJsonToList(json, Student.class);
                  for (Student st : jsonToList) {
                      System.out.println("name: " + st.getName());
                      System.out.println("age: " + st.getAge());
                      System.out.println("male: " + st.isMale());
      
                      Address address = st.getAddress();
                      System.out.println("street: " + address.getStreet());
                      System.out.println("city: " + address.getCity());
                      System.out.println("country: " + address.getCountry());
      
                      Student student = new Student(st.getName(), st.getAge(), st.isMale(), address);
                      System.out.println("student"+student.toString());
                  }
              } catch (JSONException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
          }
      
          @Test
          public void parseJSON(){
              String json="{\n" +
                      "    \"buses\": {\n" +
                      "        \"bus\": [\n" +
                      "            {\n" +
                      "                \"last_foot_dist\": \"0\",\n" +
                      "                \"time\": \"37\",\n" +
                      "                \"segments\": {\n" +
                      "                    \"segment\": [\n" +
                      "                        {\n" +
                      "                            \"line_name\": \"立珊专线(中南大学学生公寓-长沙火车站)\",\n" +
                      "                            \"foot_dist\": \"362\",\n" +
                      "                            \"stat_xys\": \"\",\n" +
                      "                            \"stats\": \"岳麓山南;湖南师大;二里半;岳麓山北;市四医院;华图教育(太平街口);牛耳教育(南阳街口);韭菜园;曙光路口;长岛路口;长沙火车站\",\n" +
                      "                            \"end_stat\": \"长沙火车站\",\n" +
                      "                            \"line_xys\": \"\",\n" +
                      "                            \"line_dist\": \"7535\",\n" +
                      "                            \"start_stat\": \"岳麓山南\"\n" +
                      "                        }\n" +
                      "                    ]\n" +
                      "                },\n" +
                      "                \"foot_dist\": \"362\",\n" +
                      "                \"dist\": \"7897\"\n" +
                      "            },\n" +
                      "            {\n" +
                      "                \"last_foot_dist\": \"0\",\n" +
                      "                \"time\": \"41\",\n" +
                      "                \"segments\": {\n" +
                      "                    \"segment\": [\n" +
                      "                        {\n" +
                      "                            \"line_name\": \"旅1路(科教新村-长沙火车站)\",\n" +
                      "                            \"foot_dist\": \"337\",\n" +
                      "                            \"stat_xys\": \"\",\n" +
                      "                            \"stats\": \"岳麓山南;湖南师大;二里半;岳麓山北;市四医院;华图教育(太平街口);蔡锷中路口;韭菜园;乔庄;曙光路口;长岛路口;五一东村;车站路口;长沙火车站\",\n" +
                      "                            \"end_stat\": \"长沙火车站\",\n" +
                      "                            \"line_xys\": \"\",\n" +
                      "                            \"line_dist\": \"7822\",\n" +
                      "                            \"start_stat\": \"岳麓山南\"\n" +
                      "                        }\n" +
                      "                    ]\n" +
                      "                },\n" +
                      "                \"foot_dist\": \"337\",\n" +
                      "                \"dist\": \"8159\"\n" +
                      "            }\n" +
                      "        ]\n" +
                      "    }\n" +
                      "}";
              try {
                  JSONObject jb = JSON.parseObject(json);
                  String buses = jb.getString("buses");
                  JSONObject jb2 = JSON.parseObject(buses);
                  String bus2 = jb2.getString("bus");
                  List<BusChild> bus = JSON.parseArray(bus2, BusChild.class);
                  for (BusChild busChild : bus) {
                      List<Segment> segmentList = busChild.getSegments();
                      for (Segment segment : segmentList) {
                          
                      }
                  }
                  
                  // String bus = jb.getString("bus");
      
      
                  // Bus bus = FastJsonUtil.getJsonToBean(buses, Bus.class);
                  // BusChild busChild = bus.getBusChild();
                  // List<Segment> segments = busChild.getSegments();
                  // for (Segment segment : segments) {
                  //
                  // }
                  
      
      
                  // List<BusChild> busChildList = FastJsonUtil.getJsonToList(buses, BusChild.class);
                  // for (BusChild busChild : busChildList) {
                  //     List<Segment> segmentList = busChild.getSegments();
                  //     for (Segment segment : segmentList) {
                  //
                  //     }
                  // }
                  //
                  // JSONObject jb2 = JSON.parseObject(buses);
                  //
                  // String bus = jb2.getString("bus");
      
                  // JSONObject jb3 = JSON.parseObject(bus);
                  // String segments = jb3.getString("segments");
                  //
                  // List<Segment> jsonToList = FastJsonUtil.getJsonToList(segments, Segment.class);
                  //
                  // for (Segment segment : jsonToList) {
                  //
                  // }
                  // for (BusChild busChild : jsonToList) {
                  //     System.out.println(""+busChild.getLastFootDist());
                  //     System.out.println(""+busChild.getTime());
                  //     System.out.println(""+busChild.getFootDist());
                  //     System.out.println(""+busChild.getDist());
                  //
                  //     List<Segments> segmentsList = busChild.getSegments();
                  //     for (Segments segment : segmentsList) {
                  //         System.out.println("line_name: "+segment.getLineName());
                  //         System.out.println("foot_dist: "+segment.getFootDist());
                  //         System.out.println("stat_xys: "+segment.getStatXys());
                  //         System.out.println("stats: "+segment.getStats());
                  //         System.out.println("end_stat: "+segment.getEndStat());
                  //         System.out.println("line_xys: "+segment.getLineXys());
                  //         System.out.println("line_dist: "+segment.getLineDist());
                  //         System.out.println("start_stat: "+segment.getStartStat());
                  //     }
                  //
                  // }
              } catch (JSONException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
          }
      
          //序列化时指定json的key值
          public static void main(String[] args) {
              Segment build = new Segment().builder()
                      .foot_dist("123")
                      .start_stat("456")
                      .stats("https://files.jxasp.com/image/2").build();
              System.out.println("sss"+JSON.toJSONString(build));
      
          }
      }
      
      
        6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
      • 25
      • 26
      • 27
      • 28
      • 29
      • 30
      • 31
      • 32
      • 33
      • 34
      • 35
      • 36
      • 37
      • 38
      • 39
      • 40
      • 41
      • 42
      • 43
      • 44
      • 45
      • 46
      • 47
      • 48
      • 49
      • 50
      • 51
      • 52
      • 53
      • 54
      • 55
      • 56
      • 57
      • 58
      • 59
      • 60
      • 61
      • 62
      • 63
      • 64
      • 65
      • 66
      • 67
      • 68
      • 69
      • 70
      • 71
      • 72
      • 73
      • 74
      • 75
      • 76
      • 77
      • 78
      • 79
      • 80
      • 81
      • 82
      • 83
      • 84
      • 85
      • 86
      • 87
      • 88
      • 89
      • 90
      • 91
      • 92
      • 93
      • 94
      • 95
      • 96
      • 97
      • 98
      • 99
      • 100
      • 101
      • 102
      • 103
      • 104
      • 105
      • 106
      • 107
      • 108
      • 109
      • 110
      • 111
      • 112
      • 113
      • 114
      • 115
      • 116
      • 117
      • 118
      • 119
      • 120
      • 121
      • 122
      • 123
      • 124
      • 125
      • 126
      • 127
      • 128
      • 129
      • 130
      • 131
      • 132
      • 133
      • 134
      • 135
      • 136
      • 137
      • 138
      • 139
      • 140
      • 141
      • 142
      • 143
      • 144
      • 145
      • 146
      • 147
      • 148
      • 149
      • 150
      • 151
      • 152
      • 153
      • 154
      • 155
      • 156
      • 157
      • 158
      • 159
      • 160
      • 161
      • 162
      • 163
      • 164
      • 165
      • 166
      • 167
      • 168
      • 169
      • 170
      • 171
      • 172
      • 173
      • 174
      • 175
      • 176
      • 177
      • 178
      • 179
      • 180
      • 181
      • 182
      • 183
      • 184
      • 185
      • 186
      • 187
      • 188
      • 189
      • 190
      • 191
      • 192
      • 193
      • 194
      • 195
      • 196
      • 197
      • 198
      • 199
      • 200
      • 201
      • 202
      • 203
      • 204
      • 205
      • 206
      • 207
      • 208
      • 209
      • 210
      • 211
      • 212
      • 213
      • 214
      • 215
      • 216
      • 217
      • 218
      • 219
      • 220
      • 221
      • 222
      • 223
      • 224
      • 225
      • 226
      • 227
      • 228
      • 229
      • 230
      • 231
      • 232
      • 233
      • 234
      • 235
      • 236
      • 237
      • 238
      • 239
      • 240
      • 241
      • 242
      • 243
      • 244
      • 245
      • 246
      • 247
      • 248
      • 249
      • 250
      • 251
      • 252
      • 253
      • 254
      • 255
      • 256
      • 257
      • 258
      • 259
      • 260
      • 261
      • 262
      • 263
      • 264
      • 265
      • 266
      • 267
      • 268
      • 269
      • 270
      • 271
      • 272
      • 273
      • 274
      • 275
      • 276
      • 277
      • 278
      • 279
      • 280
      • 281
      • 282
      • 283
      • 284
      • 285
      • 286
      • 287
      • 288
      • 289
      • 290
      • 291
      • 292
      • 293
      • 294
      • 295
      • 296
      • 297
      • 298
      • 299
      • 300
      • 301
      • 302
      • 303
      • 304
      • 305
      • 306
      • 307
      • 308
      • 309
      • 310
      • 311
      • 312
      • 313
      • 314
      • 315
      • 316
      • 317
      • 318
      • 319
      • 320
      • 321
      • 322
      • 323
      • 324

      `package com.gblfy.utils;

      import com.alibaba.fastjson.JSON;
      import com.alibaba.fastjson.JSONArray;
      import com.alibaba.fastjson.TypeReference;

      import java.util.List;
      import java.util.Map;

      public class FastJsonUtil {

      /**
       * 功能描述:把JSON数据转换成指定的java对象
       *
       * @param jsonData JSON数据
       * @param clazz    指定的java对象
       * @return 指定的java对象
       */
      public static <T> T getJsonToBean(String jsonData, Class<T> clazz) {
          return JSON.parseObject(jsonData, clazz);
      }
      
      /**
       * 功能描述:把java对象转换成JSON数据
       *
       * @param object java对象
       * @return JSON数据
       */
      public static String getBeanToJson(Object object) {
          return JSON.toJSONString(object);
      }
      
      /**
       * 功能描述:把JSON数据转换成指定的java对象列表
       *
       * @param jsonData JSON数据
       * @param clazz    指定的java对象
       * @return List<T>
       */
      public static <T> List<T> getJsonToList(String jsonData, Class<T> clazz) {
          return JSON.parseArray(jsonData, clazz);
      }
      
      /**
       * 功能描述:把JSON数据转换成较为复杂的List<Map<String, Object>>
       *
       * @param jsonData JSON数据
       * @return List<Map < String, Object>>
       */
      public static List<Map<String, Object>> getJsonToListMap(String jsonData) {
          return JSON.parseObject(jsonData, new TypeReference<List<Map<String, Object>>>() {
          });
      }
      
      /**
       * List<T> 转 json 保存到数据库
       */
      public static <T> String listToJson(List<T> ts) {
          String jsons = JSON.toJSONString(ts);
          return jsons;
      }
      
      /**
       * 两个类之间值的转换
       * 从object》》tClass
       *
       * @param object 有数据的目标类
       * @param tClass 转换成的类
       * @param <T>
       * @return 返回tClass类
       */
      public static <T> T getObjectToClass(Object object, Class<T> tClass) {
          String json = getBeanToJson(object);
          return getJsonToBean(json, tClass);
      }
      
      /**
       * json 转 List<T>
       */
      public static <T> List<T> jsonToList(String jsonString, Class<T> clazz) {
          @SuppressWarnings("unchecked")
          List<T> ts = JSONArray.parseArray(jsonString, clazz);
          return ts;
      }
      
        6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
      • 25
      • 26
      • 27
      • 28
      • 29
      • 30
      • 31
      • 32
      • 33
      • 34
      • 35
      • 36
      • 37
      • 38
      • 39
      • 40
      • 41
      • 42
      • 43
      • 44
      • 45
      • 46
      • 47
      • 48
      • 49
      • 50
      • 51
      • 52
      • 53
      • 54
      • 55
      • 56
      • 57
      • 58
      • 59
      • 60
      • 61
      • 62
      • 63
      • 64
      • 65
      • 66
      • 67
      • 68
      • 69
      • 70
      • 71
      • 72
      • 73

      }`、

      相关技术文章

      点击QQ咨询
      开通会员
      返回顶部
      ×
      微信扫码支付
      微信扫码支付
      确定支付下载
      请使用微信描二维码支付
      ×

      提示信息

      ×

      选择支付方式

      • 微信支付
      • 支付宝付款
      确定支付下载