Json样例
{
"result":{
"realtime":{
"wid":"00",
"temperature":"33",
"direct":"东南风",
"aqi":"60",
"humidity":"61",
"power":"2级",
"info":"晴"
},
"city":"南昌",
"future":[
{
"date":"2019-07-31",
"wid":{
"night":"00",
"day":"01"
},
"temperature":"29/37℃",
"weather":"多云转晴",
"direct":"持续无风向"
},
{
"date":"2019-08-01",
"wid":{
"night":"00",
"day":"00"
},
"temperature":"28/37℃",
"weather":"晴",
"direct":"持续无风向"
},
{
"date":"2019-08-02",
"wid":{
"night":"00",
"day":"00"
},
"temperature":"29/37℃",
"weather":"晴",
"direct":"持续无风向"
},
{
"date":"2019-08-03",
"wid":{
"night":"00",
"day":"00"
},
"temperature":"29/37℃",
"weather":"晴",
"direct":"东风"
},
{
"date":"2019-08-04",
"wid":{
"night":"00",
"day":"00"
},
"temperature":"29/37℃",
"weather":"晴",
"direct":"东风"
}
]
},
"reason":"查询成功!",
"error_code":0
}
对应输出结果
import org.json.JSONArray;
import org.json.JSONObject;
public class Hello_Json {
public static void main(String[] args) {
String str = "{\n" +
" \"reason\":\"查询成功!\",\n" +
" \"result\":{\n" +
" \"city\":\"南昌\",\n" +
" \"realtime\":{\n" +
" \"temperature\":\"33\",\n" +
" \"humidity\":\"61\",\n" +
" \"info\":\"晴\",\n" +
" \"wid\":\"00\",\n" +
" \"direct\":\"东南风\",\n" +
" \"power\":\"2级\",\n" +
" \"aqi\":\"60\"\n" +
" },\n" +
" \"future\":[\n" +
" {\n" +
" \"date\":\"2019-07-31\",\n" +
" \"temperature\":\"29/37℃\",\n" +
" \"weather\":\"多云转晴\",\n" +
" \"wid\":{\n" +
" \"day\":\"01\",\n" +
" \"night\":\"00\"\n" +
" },\n" +
" \"direct\":\"持续无风向\"\n" +
" },\n" +
" {\n" +
" \"date\":\"2019-08-01\",\n" +
" \"temperature\":\"28/37℃\",\n" +
" \"weather\":\"晴\",\n" +
" \"wid\":{\n" +
" \"day\":\"00\",\n" +
" \"night\":\"00\"\n" +
" },\n" +
" \"direct\":\"持续无风向\"\n" +
" },\n" +
" {\n" +
" \"date\":\"2019-08-02\",\n" +
" \"temperature\":\"29/37℃\",\n" +
" \"weather\":\"晴\",\n" +
" \"wid\":{\n" +
" \"day\":\"00\",\n" +
" \"night\":\"00\"\n" +
" },\n" +
" \"direct\":\"持续无风向\"\n" +
" },\n" +
" {\n" +
" \"date\":\"2019-08-03\",\n" +
" \"temperature\":\"29/37℃\",\n" +
" \"weather\":\"晴\",\n" +
" \"wid\":{\n" +
" \"day\":\"00\",\n" +
" \"night\":\"00\"\n" +
" },\n" +
" \"direct\":\"东风\"\n" +
" },\n" +
" {\n" +
" \"date\":\"2019-08-04\",\n" +
" \"temperature\":\"29/37℃\",\n" +
" \"weather\":\"晴\",\n" +
" \"wid\":{\n" +
" \"day\":\"00\",\n" +
" \"night\":\"00\"\n" +
" },\n" +
" \"direct\":\"东风\"\n" +
" }\n" +
" ]\n" +
" },\n" +
" \"error_code\":0\n" +
"}";
// 转换为json对象
JSONObject json = new JSONObject(str);
System.out.println(json);
// 获取json字符
String reason = json.getString("reason");
System.out.println(reason);
// 获取json内的json对象
JSONObject result = json.getJSONObject("result");
System.out.println(result);
JSONObject realtime = result.getJSONObject("realtime");
System.out.println(realtime);
String wid = realtime.getString("wid");
System.out.println(wid);
// 获取json内的json数组
JSONArray future = result.getJSONArray("future");
System.out.println(future);
// 读取json数组,转化为object
JSONObject future_1 = future.getJSONObject(1);
System.out.println(future_1);
// 读取json字符
String data_1 = future_1.getString("date");
System.out.println(data_1);
}
}