zoukankan      html  css  js  c++  java
  • Java 解析json数据

    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<jsonArr.length();i++)
            {
                JSONObject tagValue = jsonArr.getJSONObject(i);
                String tagkey = tagValue.getString("tagkey");
                String unitkey = tagValue.getString("unitkey");
                System.out.println(tagkey+" , "+ unitkey);
            }
            return null;
        }
    }

    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<jsonArray.length();i++){
                jsonObject = jsonArray.getJSONObject(i);
                String str = jsonObject.getString("text");
                System.out.println(str);
            }    
  • 相关阅读:
    LINQ to XML 示例(转)
    (jQuery,Highcharts)前端图表系列之一 Highcharts (转)
    c#中Split等分割字符串的几种方法(转)
    Highcharts 强大的jQuery图表制作功能
    OpenFileDialog、SaveFileDialog常用屬性、對話框用法及得到系統特殊文件夾路徑(转)
    C# Regex 深入正则表达式(转)
    C#文件IO操作(转)
    免费Google地图API使用说明(转)
    Java遍历集合的几种方法分析(实现原理、算法性能、适用场合)
    C++ Web 开发框架 (CppCMS)
  • 原文地址:https://www.cnblogs.com/chuijingjing/p/11195985.html
Copyright © 2011-2022 走看看