java解析json的操作

import java.io.FileNotFoundException;
import java.io.FileReader; import com.google.gson.JsonArray;
import com.google.gson.JsonIOException;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonSyntaxException; public class ReadJSON {
public static void main(String args[]){
try { JsonParser parser=new JsonParser(); //创建JSON解析器 //JsonObject result=parser.get("result").getAsJsonObject(); JsonObject object=(JsonObject) parser.parse(new FileReader("weather.json")); //创建JsonObject对象
//+++++++++++++++++++++++++++是json中的主变量+++++++++++++++++++++++++++
System.out.println("resultcode"+object.get("resultcode").getAsInt());
System.out.println("reason"+object.get("reason").getAsString());
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ //+++++++++++++++++++++到主json的目录result中 包括 迭代json object 和json 数组
JsonObject result1=object.get("result").getAsJsonObject(); //++++++++++++++++++进入到result中的子object json之中++++++++++++++++++++++++++++++++++++++++++++++++++
JsonObject today=result1.get("today").getAsJsonObject();
System.out.println("temperature:"+today.get("temperature").getAsString());
System.out.println("weather:"+today.get("weather").getAsString());
System.out.println("city"+today.get("city").getAsString());
//today下面的迭代weather_id
JsonObject weather_id=today.get("weather_id").getAsJsonObject();
System.out.println("favalue:"+weather_id.get("fa").getAsString());
//+++++++++++++++++通过result1 找到 存储数据的子目录 ARRAY future,
JsonArray array=result1.get("future").getAsJsonArray(); //得到为json的数组
for(int i=0;i<array.size();i++){
System.out.println("---------------");
JsonObject subObject=array.get(i).getAsJsonObject();
System.out.println("id="+subObject.get("temperature").getAsString());
System.out.println("name="+subObject.get("weather").getAsString());
System.out.println("week="+subObject.get("week").getAsString());
System.out.println("wind"+subObject.get("wind").getAsString());
//System.out.println("ide="+subObject.get("ide").getAsString());
} } catch (JsonIOException e) {
e.printStackTrace();
} catch (JsonSyntaxException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}

他所解析的json为

 {
"resultcode": "200",
"reason": "successed!",
"result": {
"sk": {
"temp": "24",
"wind_direction": "西南风",
"wind_strength": "2级",
"humidity": "51%",
"time": "10:11"
},
"today": {
"temperature": "16℃~27℃",
"weather": "阴转多云",
"weather_id": {
"fa": "02",
"fb": "01"
},
"wind": "西南风3-4 级",
"week": "星期四",
"city": "滨州",
"date_y": "2015年06月04日",
"dressing_index": "舒适",
"dressing_advice": "建议着长袖T恤、衬衫加单裤等服装。年老体弱者宜着针织长袖衬衫、马甲和长裤。",
"uv_index": "最弱",
"comfort_index": "",
"wash_index": "较适宜",
"travel_index": "",
"exercise_index": "较适宜",
"drying_index": ""
},
"future": [
{
"temperature": "16℃~27℃",
"weather": "阴转多云",
"weather_id": {
"fa": "02",
"fb": "01"
},
"wind": "西南风3-4 级",
"week": "星期四",
"date": "20150604"
},
{
"temperature": "20℃~32℃",
"weather": "多云转晴",
"weather_id": {
"fa": "01",
"fb": "00"
},
"wind": "西风3-4 级",
"week": "星期五",
"date": "20150605"
},
{
"temperature": "23℃~35℃",
"weather": "多云转阴",
"weather_id": {
"fa": "01",
"fb": "02"
},
"wind": "西南风3-4 级",
"week": "星期六",
"date": "20150606"
},
{
"temperature": "20℃~33℃",
"weather": "多云",
"weather_id": {
"fa": "01",
"fb": "01"
},
"wind": "北风微风",
"week": "星期日",
"date": "20150607"
},
{
"temperature": "22℃~34℃",
"weather": "多云",
"weather_id": {
"fa": "01",
"fb": "01"
},
"wind": "西南风3-4 级",
"week": "星期一",
"date": "20150608"
},
{
"temperature": "22℃~33℃",
"weather": "阴",
"weather_id": {
"fa": "02",
"fb": "02"
},
"wind": "西南风3-4 级",
"week": "星期二",
"date": "20150609"
},
{
"temperature": "22℃~33℃",
"weather": "多云",
"weather_id": {
"fa": "01",
"fb": "01"
},
"wind": "南风3-4 级",
"week": "星期三",
"date": "20150610"
}
]
}
}
上一篇:(小组)第六次作业:NABCD模型分析。产品Backlog。


下一篇:感悟优化——Netty对JDK缓冲区的内存池零拷贝改造