示例一:
JSON文件
{
"reason":"成功的返回",
"result":{
"stat":"1",
"data":[
{
"uniquekey":"4aa8ba3a720a12f1ac9b0ea2b7c1bb8b",
"title":"中国贸促会:加强投资 推动“一带一路”建设",
"date":"2018-04-12 18:09",
"category":"头条",
"author_name":"央视网",
},
{
"uniquekey":"d53883da51e856c2c96c8b219c6d06e7",
"title":"冯俊:人类命运共同体 全球治理的中国方案",
"date":"2018-04-12 16:06",
"category":"头条",
}
]
},
}
程序:
//第一步,string参数相当于一个JSON,依次解析下一步
JSONObject json = new JSONObject(string);
JSONObject result = json.getJSONObject("result");
//当你获得JSONArray 类型的时候,然后要写for循环遍历获取里面每一个Object
JSONArray data = result.getJSONArray("data");
for (int i = 0; i < data.length(); i++) {
JSONObject value = data.getJSONObject(i);
//获取到title值
String title = value.getString("title");
// String title = value.optString("title");
}
示例二:
{"device":[{"dev_name":"slave1","comm_s":"0","variable":"40001":"bad","40002":"bad","40003":"bad","40004":"bad","40005":"bad"}}]}
程序:
JSON文件
try {
JSONObject json = new JSONObject(msgString);//获取一个JSONObject
JSONArray data = json.getJSONArray("device");//获取一个JSONArray
JSONObject value = data.getJSONObject(0);
String title = value.getString("dev_name");
String comm_s = value.getString("comm_s");
String variable = value.getString("variable");
JSONObject json1 = new JSONObject(variable);
String dev_40001 = json1.getString("40001");
String dev_40002 = json1.getString("40002");
String dev_40003 = json1.getString("40003");
String dev_40004 = json1.getString("40004");
String dev_40005 = json1.getString("40005");
} catch (JSONException e) {
e.printStackTrace();
}
示例三
{"device":[{"dev_name":"slave1","comm_s":"0","variable":{"DO1":"bad","DO2":"bad","DI1":"bad","DI2":"bad","AI1":"bad","AI2":"bad","AI3":"bad","AI4":"bad","AI5":"bad","AI6":"bad","AO1":"bad","AO2":"bad"}},{"dev_name":"slave2","comm_s":"0","variable":{"DO1":"bad","DO2":"bad","DI1":"bad","DI2":"bad","AI1":"bad","AI2":"bad","AI3":"bad","AI4":"bad","AI5":"bad","AI6":"bad","AO1":"bad","AO2":"bad"}}]}
程序:
try {
JSONObject json = new JSONObject(msgString);
JSONArray data = json.getJSONArray("device");
for(int i = 0;i<data.length();i++)
{
JSONObject value = data.getJSONObject(i);
//获取到title值
String dev_name = value.getString("dev_name");
String comm_s = value.getString("comm_s");
String variable = value.getString("variable");
JSONObject json1 = new JSONObject(variable);
String DO1 = json1.getString("DO1");
String DO2 = json1.getString("DO2");
String DI1 = json1.getString("DI1");
String DI2 = json1.getString("DI2");
String AI1 = json1.getString("AI1");
String AI2 = json1.getString("AI2");
String AI3 = json1.getString("AI3");
String AI4 = json1.getString("AI4");
String AI5 = json1.getString("AI5");
String AI6 = json1.getString("AI6");
String AO1 = json1.getString("AO1");
String AO2 = json1.getString("AO2");
Log.d("lcc","dev_name:"+dev_name);
Log.d("lcc","comm_s:"+comm_s);
Log.d("lcc","DO1:"+DO1);
Log.d("lcc","DO2:"+DO2);
Log.d("lcc","DI1:"+DI1);
Log.d("lcc","DI2:"+DI2);
Log.d("lcc","AI1:"+AI1);
Log.d("lcc","AI2:"+AI2);
Log.d("lcc","AI3:"+AI3);
Log.d("lcc","AI4:"+AI4);
Log.d("lcc","AI5:"+AI5);
Log.d("lcc","AI6:"+AI6);
Log.d("lcc","AO1:"+AO1);
Log.d("lcc","AO2:"+AO2);
}
} catch (JSONException e) {
e.printStackTrace();
}