解析JSON数据.docx
- 文档编号:9732802
- 上传时间:2023-02-06
- 格式:DOCX
- 页数:19
- 大小:18.79KB
解析JSON数据.docx
《解析JSON数据.docx》由会员分享,可在线阅读,更多相关《解析JSON数据.docx(19页珍藏版)》请在冰豆网上搜索。
解析JSON数据
生活指南原始JSON数据:
{
"results":
[
{
"location":
{
"id":
"WHX38U36B3BU",
"name":
"大理",
"country":
"CN",
"path":
"大理,大理,云南,中国",
"timezone":
"Asia/Shanghai",
"timezone_offset":
"08:
00"
},
"suggestion":
{
"car_washing":
{
"brief":
"不宜",
"details":
"不宜洗车,未来24小时内有雨,如果在此期间洗车,雨水和路上的泥水可能会再次弄脏您的爱车。
"
},
"dressing":
{
"brief":
"舒适",
"details":
"建议着长袖T恤、衬衫加单裤等服装。
年老体弱者宜着针织长袖衬衫、马甲和长裤。
"
},
"flu":
{
"brief":
"少发",
"details":
"各项气象条件适宜,无明显降温过程,发生感冒机率较低。
"
},
"sport":
{
"brief":
"较不宜",
"details":
"有降水,推荐您在室内进行健身休闲运动;若坚持户外运动,须注意保暖并携带雨具。
"
},
"travel":
{
"brief":
"适宜",
"details":
"有降水,温度适宜,在细雨中游玩别有一番情调,可不要错过机会呦!
但记得出门要携带雨具。
"
},
"uv":
{
"brief":
"弱",
"details":
"紫外线强度较弱,建议出门前涂擦SPF在12-15之间、PA的防晒护肤品。
"
}
},
"last_update":
"2016-06-10T16:
40:
1008:
00"
}
]
}
实时天气情况原始JSON数据:
{
"results":
[
{
"location":
{
"id":
"WHX38U36B3BU",
"name":
"大理",
"country":
"CN",
"path":
"大理,大理,云南,中国",
"timezone":
"Asia/Shanghai",
"timezone_offset":
"08:
00"
},
"now":
{
"text":
"阵雨",
"code":
"10",
"temperature":
"19"
},
"last_update":
"2016-06-10T16:
25:
0008:
00"
}
]
}
未来几天天气预报原始JSON数据:
{
"results":
[
{
"location":
{
"id":
"WHX38U36B3BU",
"name":
"大理",
"country":
"CN",
"path":
"大理,大理,云南,中国",
"timezone":
"Asia/Shanghai",
"timezone_offset":
"08:
00"
},
"daily":
[
{
"date":
"2016-06-10",
"text_day":
"阵雨",
"code_day":
"10",
"text_night":
"阵雨",
"code_night":
"10",
"high":
"24",
"low":
"16",
"precip":
"",
"wind_direction":
"无持续风向",
"wind_direction_degree":
"",
"wind_speed":
"10",
"wind_scale":
"2"
},
{
"date":
"2016-06-11",
"text_day":
"阵雨",
"code_day":
"10",
"text_night":
"小雨",
"code_night":
"13",
"high":
"22",
"low":
"17",
"precip":
"80",
"wind_direction":
"无持续风向",
"wind_direction_degree":
"",
"wind_speed":
"10",
"wind_scale":
"2"
},
{
"date":
"2016-06-12",
"text_day":
"中雨",
"code_day":
"14",
"text_night":
"小雨",
"code_night":
"13",
"high":
"24",
"low":
"17",
"precip":
"90",
"wind_direction":
"无持续风向",
"wind_direction_degree":
"",
"wind_speed":
"10",
"wind_scale":
"2"
}
],
"last_update":
"2016-06-10T11:
00:
0008:
00"
}
]
}
解析JSON:
packagecom.lastexam.weatherforecast;
importjava.util.ArrayList;
importjava.util.Scanner;
importnet.sf.json.JSONArray;
importnet.sf.json.JSONObject;
publicclassParseJSON
{
/*
publicstaticvoidmain(String[]args)
{
Scannerin=newScanner(System.in);
System.out.println("请输入数据:
");
Stringstring=in.next();
System.out.println(ParseResult(string));
Locationlocation=newLocation();
location=ParseLocation(ParseResult(string));
System.out.println(location.toString());
//实时天气情况解析完毕
RealNownow=newRealNow();
now=ParseNow(ParseResult(string));
System.out.println(now.toString());
//天气预报解析完毕
WeatherDaily[]daily=ParseDaily(ParseResult(string));
for(inti=0;i { System.out.println(daily[i].toString()); } //suggestion解析完毕 SuggestionCarWashingcarWashing=newSuggestionCarWashing(); carWashing=ParseCarWashing(ParseResult(string)); System.out.println(carWashing.toString()); SuggestionDressingdressing=newSuggestionDressing(); dressing=ParseDressing(ParseResult(string)); System.out.println(dressing.toString()); SuggestionFluflu=newSuggestionFlu(); flu=ParseFlu(ParseResult(string)); System.out.println(flu.toString()); SuggestionSportsport=newSuggestionSport(); sport=ParseSport(ParseResult(string)); System.out.println(sport.toString()); SuggestionTraveltravel=newSuggestionTravel(); travel=ParseTravel(ParseResult(string)); System.out.println(travel.toString()); SuggestionUvuv=newSuggestionUv(); uv=ParseUv(ParseResult(string)); System.out.println(uv.toString()); System.out.println(ParseUpdateTime(ParseResult(string))); } */ //将原始的JSON数据解析之后返回(主要是提取result中的内容) publicStringParseResult(StringoriginalJSON) { JSONObjectresultsJSONObject=JSONObject.fromObject(originalJSON); JSONArrayresultsJSONArray=resultsJSONObject.getJSONArray("results"); returnresultsJSONArray.getJSONObject(0).toString(); } //提取location中的内容,并且将提取出来的内容放到Location对象中并返回 publicLocationParseLocation(StringresultJSON) { Locationlocation=newLocation(); JSONObjectlocationJSONObjectOriginal=JSONObject.fromObject(resultJSON); StringlocationString=locationJSONObjectOriginal.getString("location"); JSONObjectlocationJSONObject=JSONObject.fromObject(locationString); location.setId(locationJSONObject.getString("id")); location.setName(locationJSONObject.getString("name")); location.setContry(locationJSONObject.getString("country")); location.setPath(locationJSONObject.getString("path")); location.setTimezone(locationJSONObject.getString("timezone")); location.setTimezone_offset(locationJSONObject.getString("timezone_offset")); returnlocation; } //提取实时天气情况中的now的内容,并将提取出来的内容放入到RealNow的对象里面并返回 publicRealNowParseNow(StringresultJSON) { RealNownow=newRealNow(); JSONObjectnowJSONObjectOriginal=JSONObject.fromObject(resultJSON); StringnowString=nowJSONObjectOriginal.getString("now"); JSONObjectnowJSONObject=JSONObject.fromObject(nowString); now.setText(nowJSONObject.getString("text")); now.setCode(nowJSONObject.getString("code")); now.setTemperature(nowJSONObject.getString("temperature")); returnnow; } //提取天气预报中的daily的内容,并将提取出来的内容放入到WeatherDaily的对象里面并返回 publicWeatherDaily[]ParseDaily(StringresultJSON) { WeatherDaily[]daily=newWeatherDaily[3]; daily[0]=newWeatherDaily(); daily[1]=newWeatherDaily(); daily[2]=newWeatherDaily(); JSONObjectdailyJSONObjectOriginal=JSONObject.fromObject(resultJSON); JSONArraydailyJsonArray=dailyJSONObjectOriginal.getJSONArray("daily"); for(inti=0;i { StringdailyString=dailyJsonArray.get(i).toString(); JSONObjectdailyJSONObject=JSONObject.fromObject(dailyString); daily[i].setDate(dailyJSONObject.getString("date")); daily[i].setText_day(dailyJSONObject.getString("text_day")); daily[i].setCode_day(dailyJSONObject.getString("code_day")); daily[i].setText_night(dailyJSONObject.getString("text_night")); daily[i].setCode_night(dailyJSONObject.getString("code_night")); daily[i].setHigh(dailyJSONObject.getString("high")); daily[i].setLow(dailyJSONObject.getString("low")); daily[i].setPrecip(dailyJSONObject.getString("precip")); daily[i].setWind_direction(dailyJSONObject.getString("wind_direction")); daily[i].setWind_direction_degree(dailyJSONObject.getString("wind_direction_degree")); daily[i].setWind_speed(dailyJSONObject.getString("wind_speed")); daily[i].setWind_scale(dailyJSONObject.getString("wind_scale")); } returndaily; } //提取suggestion中的car_washing的内容,并且将内容放入SuggestionCarWashing的对象之中,并返回 publicSuggestionCarWashingParseCarWashing(StringresultJSON) { SuggestionCarWashingcarWashing=newSuggestionCarWashing(); JSONObjectsuggestionJSONObjectOriginal=JSONObject.fromObject(resultJSON); StringsuggestionString=suggestionJSONObjectOriginal.getString("suggestion"); JSONObjectcarWashingJSONObjectOriginal=JSONObject.fromObject(suggestionString); StringcarWashingString=carWashingJSONObjectOriginal.getString("car_washing"); JSONObjectcarWashingJSONObject=JSONObject.fromObject(carWashingString); carWashing.setBrief(carWashingJSONObject.getString("brief")); carWashing.setDetails(carWashingJSONObject.getString("details")); returncarWashing; } //提取suggestion中的dressing的内容,并且将内容放入SuggestionDressing的对象之中,并返回 publicSuggestionDressingParseDressing(StringresultJSON) { SuggestionDressingdressing=newSuggestionDressing(); JSONObjectsuggestionJSONObjectOriginal=JSONObject.fromObject(resultJSON); StringsuggestionString=suggestionJSONObjectOriginal.getString("suggestion"); JSONObjectdressingJSONObjectOriginal=JSONObject.fromObject(suggestionString); StringdressingString=dressingJSONObjectOriginal.getString("dressing"); JSONObjectdressingJSONObject=JSONObject.fromObject(dressingString); dressing.setBrief(dressingJSONObject.getString("brief")); dressing.setDetails(dressingJSONObject.getString("details")); returndressing; } //提取suggestion中的flu的内容,并且将内容放入SuggestionFlu的对象之中,并返回 publicSuggestionFluParseFlu(StringresultJSON) { SuggestionFluflu=newSuggestionFlu(); JSONObjectsuggestionJSONObjectOriginal=JSONObject.fromObject(resultJSON); StringsuggestionString=suggestionJSONObjectOriginal.getString("suggestion"); JSONObjectfluJSONObjectOriginal=JSONObject.fromObject(suggestionString); StringfluString=fluJSONObjectOriginal.getString("flu"); JSONObjectfluJSONObject=JSONObject.fromObject(fluString); flu.setBrief(fluJSONObject.getString("brief")); flu.setDetails(fluJSONObject.getString("details")); returnflu; } //提取suggestion中的sport的内容,并且将内容放入SuggestionSport的对象之中,并返回 publicSuggestionSportParseSport(StringresultJSON) { SuggestionSportsport=newSuggestionSport(); JSONObjectsuggestionJSONObjectOriginal=JSONObject.fromObject(resultJSON); StringsuggestionString=suggestionJSONObjectOriginal.getString("suggestion"); JSONObjectsportJSONObjectOriginal=JSONObject.fromObject(suggestionString); StringsportString=sportJSONObjectOriginal.getString("sport"); JSONObjectsportJSONObject=JSONObject.fromObject(sportString); sport.setBrief(sportJSONObject.getString("brief")); sport.setDetails(sportJSONObject.getString("details")); returnsport; } //提取suggestion中的travel的内容,并且将内容放入SuggestionTravel的对象之中,并返回 publicSugg
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 解析 JSON 数据
![提示](https://static.bdocx.com/images/bang_tan.gif)