您的位置:首页 > Web前端 > JavaScript

fastjson解析

2016-07-13 10:55 393 查看
引入fastjson jar 包

<!-- json -->

<dependency>

    <groupId>com.alibaba</groupId>

    <artifactId>fastjson</artifactId>

    <version>1.1.41</version>

</dependency>

解析代码:

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"));

:

简单的解析:

   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());

       }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: