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);
        }    
原文地址:https://www.cnblogs.com/chuijingjing/p/11195985.html