博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java 解析json数据
阅读量:5047 次
发布时间:2019-06-12

本文共 2918 字,大约阅读时间需要 9 分钟。

1.依赖jar包

import org.json.JSONArray;import org.json.JSONObject;

2.代码实例(一)

json字符串的格式是{k:v, k:v, k:[{k:v},{k:v},{k:v}]}

{    "totalpages":"1",    "currpage":"1",    "totalrecords":"31",    "griddata":[        {            "tagkey":"YK011011012",            "unitkey":"YK01101",            "timestamp":"2019/7/16 10:24:29",            "value":"22.9",            "tagstatus":"0",            "sysgroup":"安全监测监控系统",            "createtime":"2019/7/16 10:24:29",            "unitname":"南屯煤矿",            "groupkey":"YK01",            "sortnum":"1",            "tagname":"三采避难硐室外温度",            "description":"",            "tagtype":"温度",            "engunit":"℃",            "datatype":"1",            "minraw":"45",            "maxraw":"-5",            "loalarmlimit":"35",            "hialarmlimit":"0"        },        {            "tagkey":"YK011011225",            "unitkey":"YK01101",            "timestamp":"2019/7/16 10:24:29",            "value":"28.2",            "tagstatus":"0",            "sysgroup":"安全监测监控系统",            "createtime":"2019/7/16 10:24:29",            "unitname":"南屯煤矿",            "groupkey":"YK01",            "sortnum":"1",            "tagname":"七采西部变电所温度",            "description":"",            "tagtype":"温度",            "engunit":"℃",            "datatype":"1",            "minraw":"45",            "maxraw":"-5",            "loalarmlimit":"34",            "hialarmlimit":"0"        }    ]}

代码解析:

package yk.bigdata.dl.ias.fileParse;import org.json.JSONArray;import org.json.JSONObject;/** * @Author: cjj * @Date: Created in 10:43 2019/7/16 * @Description: 解析json数据 */public class TagValueJsonFileParse {    public static void main(String[] args) {        JsonParse();    }    public static String JsonParse(){        String jsonStr ="{'totalpages':'1','currpage':'1','totalrecords':'31','griddata':[{'tagkey':'YK011011012','unitkey':'YK01101',},{'tagkey':'YK011011225','unitkey':'YK01102',}]}";        // 因为json字符串是大括号包围,所以用JSONObject解析        JSONObject jsonObj = new JSONObject(jsonStr);        // 获取数组        JSONArray jsonArr = jsonObj.getJSONArray("griddata");        // 遍历数组里面的json        for(int i=0;i

3.代码实例(二)

json字符串的格式是:[{k:v},{k:v},{k:v}]

[     {"iconCls":"","children":[],"text":"断电状态"},     {"iconCls":"","children":[],"text":"风门"},     {"iconCls":"","children":[],"text":"风速"},     {"iconCls":"","children":[],"text":"风筒开关"},     {"iconCls":"","children":[],"text":"负压"},     {"iconCls":"","children":[],"text":"激光甲烷"},     {"iconCls":"","children":[],"text":"甲烷"},     {"iconCls":"","children":[],"text":"局扇"},     {"iconCls":"","children":[],"text":"馈电状态"},     {"iconCls":"","children":[],"text":"温度"},     {"iconCls":"","children":[],"text":"一氧化碳"},     {"iconCls":"","children":[],"text":"主扇"}]

代码解析:

JSONObject jsonObject;        JSONArray jsonArray = new JSONArray(jsonStr); // 直接解析成数组        for(int i=0;i

 

转载于:https://www.cnblogs.com/chuijingjing/p/11195985.html

你可能感兴趣的文章
[leetcode] 1. Two Sum
查看>>
iOS 日常工作之常用宏定义大全
查看>>
PHP的SQL注入技术实现以及预防措施
查看>>
MVC Razor
查看>>
软件目录结构规范
查看>>
Windbg调试Sql Server 进程
查看>>
linux调度器系列
查看>>
mysqladmin
查看>>
解决 No Entity Framework provider found for the ADO.NET provider
查看>>
SVN服务器搭建和使用(三)(转载)
查看>>
Android 自定义View (三) 圆环交替 等待效果
查看>>
设置虚拟机虚拟机中fedora上网配置-bridge连接方式(图解)
查看>>
HEVC播放器出炉,迅雷看看支持H.265
查看>>
[置顶] Android仿人人客户端(v5.7.1)——人人授权访问界面
查看>>
Eclipse 调试的时候Tomcat报错启动不了
查看>>
【安卓5】高级控件——拖动条SeekBar
查看>>
cf 821E Okabe and El Psy Kongroo(矩阵快速幂)
查看>>
优化存储过程的基本方法
查看>>
Spring如何解决循环引用
查看>>
Bootstrap学习 按钮组
查看>>