json格式
{
"code": "0",
"msg": "成功!",
"data": {
"result": [
{
"id": "1231231231",
"title": "1231231",
"img": "1232131231",
"keywords": "3434",
"description": "4234234",
"readNum": 25,
"createTime": "2017-09-11 16:30:40",
"h5Url": "234234",
"shareWords": "23423",
"source": "2342",
"isShare": "1"
},
{
"id": "4343",
"title": "地震说来就来防范重在平时",
"img": "2342342",
"keywords": "地震、防范",
"description": "在已发布破坏性地震临震预报或易发生地震的地区,应做好以下几个方面的应急工作",
"readNum": 137,
"createTime": "2017-08-23 12:03:48",
"h5Url": "34234234",
"shareWords": "在已发布破坏性地震临震预报或易发生地震的地区,应做好以下几个方面的应急工作",
"source": "234234",
"isShare": "1"
}
]
}
}
1、读取本地json文件
//获取json
public static String getJson() {
String jsonStr = "";
try {
File file = new File("E:\\test.json");
FileReader fileReader = new FileReader(file);
Reader reader = new InputStreamReader(new FileInputStream(file),"Utf-8");
int ch = 0;
StringBuffer sb = new StringBuffer();
while ((ch = reader.read()) != -1) {
sb.append((char) ch);
}
fileReader.close();
reader.close();
jsonStr = sb.toString();
return jsonStr;
} catch (Exception e) {
return null;
}
}
2、解析多层json
public static void main(String[] args) {
System.out.println(getJson());
JSONObject json = JSONObject.fromObject(getJson());
System.out.println(json.getJSONObject("data").getJSONArray("result"));
}