0
点赞
收藏
分享

微信扫一扫

[FastJson举例]-- fastjson解析

兽怪海北 2022-11-03 阅读 71


1、引入fastjson jar 包

<!-- json -->

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.1.41</version>

 

</dependency>

 

2、代码实现

String temp="{\n" +
        "    \"uuid\": \"5qF6895A04-282D-4C29-97EC-FA4BAA6D05BF\",\n" +
        "    \"counts\": 5000,\n" +
        "    \"has_alarm\": \"Y\",\n" +
        "    \"realtime_data\": [\n" +
        "        {\n" +
        "            \"id\": \"76795469-35E4-461C-AF98-5A5670593F76\",\n" +
        "            \"value\": \"30\",\n" +
        "            \"save_time\": \"20160706152202\"\n" +
        "        },\n" +
        "        {\n" +
        "            \"id\": \"B9346393-242C-4669-85C6-E392D221A586\",\n" +
        "            \"value\": \"100\",\n" +
        "            \"save_time\": \"20160706152202\"\n" +
        "        },\n" +
        "        {\n" +
        "            \"id\": \"06d5f18a-2a9a-4727-a0e4-4541a00338cb\",\n" +
        "            \"value\": \"50\",\n" +
        "            \"save_time\": \"20160706152202\"\n" +
        "        }\n" +
        "    ],\n" +
        "    \"alarm_data\": [\n" +
        "        {\n" +
        "            \"id\": \"76795875-35E4-461C-AF98-5ALIJDKYEF76\",\n" +
        "            \"value\": \"30\",\n" +
        "            \"save_time\": \"20160706152202\"\n" +
        "        },\n" +
        "        {\n" +
        "            \"id\": \"DD990A98-5654-4BBA-8FA2-D4123A5321ED\",\n" +
        "            \"value\": \"100\",\n" +
        "            \"save_time\": \"20160706152202\"\n" +
        "        }\n" +
        "    ],\n" +
        "    \"timestamp\": \"20160706152356\"\n" +
        "}";
JSONObject jsonObject= JSONObject.parseObject(temp);
//JSONObject jsonObject= JSON.parseObject(temp);
jsonObject.getString("uuid");
System.out.println( jsonObject.getString("uuid"));
JSONArray realtime_data = jsonObject.getJSONArray("realtime_data");
System.out.println(realtime_data.getJSONObject(0).getString("save_time"));
:

 

 

3、简单的解析

   JSONObject jsonObject = JSONObject.parseObject(decoderJson);


        String uuid = jsonObject.getString("uuid");
        String type= jsonObject.getString("type");
        String name= jsonObject.getString("name");
        int counts = jsonObject.getInteger("counts");


        JSONArray message = jsonObject.getJSONArray("message");//如果需要解析List时

       for (Object obj : message) {

               //解析出每一个List范型的对象
                BasicInfo basicInfo = JSON.parseObject(obj.toString(), BasicInfo.class);
                System.out.println(basicInfo.toString());
       }

举报

相关推荐

打包后 fastjson 解析时 卡住

fastjson笔记

fastjson详解

fastjson 2

fastjson fastuse

0 条评论