1、json字符串转json对象
String result = "{\"code\":\"0000\",\"message\":\"查询成功!\",
\"sentDate\":\"2019-03-15\",\"totalNum\":\"3000\",\"result\":
[\"CERT201708300057_20180109_1515464714812\",
\"CERT201708300057_20190209_1515464714811 \",\"CERT201708300057_20190309_1515464714816 \"]}";
JSONObject jsonObject=JSONObject.fromObject(result);
//如
String totalNum = jsonObject.getString("totalNum")
2、获取json数组格式中的值
获取第一点中result中数组的值
JSONArray jsonArray = null;
try {
jsonArray = new JSONArray(jsonObject.getString("result"));
for(int i=0;i<jsonArray.length();i++){
System.out.println(jsonArray.get(i));
}
} catch (JSONException e) {
e.printStackTrace();
}
输出结果为:
CERT201708300057_20180109_1515464714812
CERT201708300057_20190209_1515464714811
CERT201708300057_20190309_1515464714816
引用jar:
net.sf.json.JSONObject
com.fr.base.core.json.JSONArray
eg:获取fileId
public static void main(String[] args) {
String result = "{\"attachs\":[{\"fileId\":10000088060170,\"fileName\":\"wifi密码.png\",\"uploaderId\":\"WU_FILE_0\"},{\"fileId\":10000088060171,\"fileName\":\"ui.jpg\",\"uploaderId\":\"WU_FILE_1\"}]}";
JSONObject jsonObject=JSONObject.fromObject(result);
JSONArray jsonArray = null;
try {
jsonArray = new JSONArray(jsonObject.getString("attachs"));
for(int i=0;i<jsonArray.length();i++){
Object object = jsonArray.get(i);
jsonObject=JSONObject.fromObject(object.toString());
System.out.println(jsonObject.get("fileId"));
}
} catch (Exception e) {
e.printStackTrace();
}
}
输出结果:
10000088060170
10000088060171
网友评论