我对如何解析这个JSON很困惑。 到目前为止,这是我的方法。另请告诉我在Android中解析JSON的正确方法
JSON:
{
"latitude":37.8267,
"longitude":-122.423,
"timezone":"America/Los_Angeles",
"offset":-7,
"currently":{
"time":1443322196,
"summary":"Partly Cloudy",
"icon":"partly-cloudy-night",
"nearestStormDistance":13,
"nearestStormBearing":77,
"precipIntensity":0,
"precipProbability":0,
"temperature":63.94,
"apparentTemperature":63.94,
"dewPoint":55.46,
"humidity":0.74,
"windSpeed":8.59,
"windBearing":277,
"visibility":8.51,
"cloudCover":0.44,
"pressure":1010.39,
"ozone":261.48
},
"minutely":{
"summary":"Partly cloudy for the hour.",
"icon":"partly-cloudy-night",
"data":[
{
"time":1443322140,
"precipIntensity":0,
"precipProbability":0
},
}
现在正在解析“当前”对象,但是当我尝试解析“微小”对象时,它在Logcat中没有显示任何值
这是我的代码:
JSONObject forecast = new JSONObject(jsonData);
JSONArray summary = new JSONArray(jsonData);
String timezone = forecast.getString("timezone");
String city = getLocationName(forecast.getDouble("latitude"), forecast.getDouble("longitude"));
JSONObject currently = forecast.getJSONObject("currently");
JSONArray hour = summary.getJSONArray("minutely");
for (int i = 0; i < hour.length(); i++) {
JSONObject jsonObject = hour.getJSONObject(i);
String summary = jsonObject.getString("summary");
}
CurrentWeather currentWeather = new CurrentWeather();
currentWeather.setHumidity(currently.getDouble("humidity"));
currentWeather.setTime(currently.getLong("time"));
currentWeather.setIcon(currently.getString("icon"));
currentWeather.setPrecipChance(currently.getDouble("precipProbability"));
currentWeather.setTemp(currently.getDouble("temperature"));
currentWeather.setTimezone(timezone);
currentWeather.setLocation(city);
答案 0 :(得分:2)
根据您在评论中添加的端点网址的响应,解析将如下所示
JSONObject forecast = new JSONObject(jsonData);
double latitude = forecast.getDouble("latitude");
double longitude= forecast.getDouble("longitude");
String timezone = forecast.getString("timezone");
JSONObject jsonObjCurrently= forecast.getJSONObject("currently");
//parse string, long and double objects within jsonObjCurrently accordingly
JSONObject jsonObjMinutely= forecast.getJSONObject("minutely");
String summary= jsonObjMinutely.getString("summary");
String icon= jsonObjMinutely.getString("icon");
JSONArray jsonArrayMinutelyData = jsonObjMinutely.getJSONArray("data");
for(int i=0; i<jsonArrayMinutelyData .length(); i++){
JSONObject tempData = jsonArrayMinutelyData.get(i);
long time = tempData.getLong("time");
//parse the remaining object pairs.
}
JSONObject jsonObjHourly= forecast.getJSONObject("hourly");
//similar to minutely parsing. Only has more and different data
JSONObject jsonObjDaily= forecast.getJSONObject("daily");
//similar to hourly parsing.
JSONObject jsonObjFlags= forecast.getJSONObject("flags");
//It has 5 array and 1 string object so parse accordingly.
我添加了解析逻辑,请相应保存数据并使用它。
答案 1 :(得分:1)
您可以在错误的路径上解析minutely
。见下面的代码(测试过)。
try {
// jsonString from https://api.forecast.io/forecast/8162461ea194cb97c80209d6edf4df94/37.8267,-122.423
String jsonString = "";
JSONObject jsonObject = new JSONObject(jsonString);
JSONObject minutely = jsonObject.getJSONObject("minutely");
Log.d("JSON", "minutely: " + minutely);
String summary = minutely.getString("summary");
Log.d("JSON", "summary: " + summary);
JSONArray datas = minutely.getJSONArray("data");
for (int i = 0; i < datas.length(); i++) {
JSONObject data = datas.getJSONObject(i);
Log.d("JSON", "data @ index" + i + ": " + data);
}
} catch (JSONException e) {
e.printStackTrace();
}
输出:
D/JSON ( 1590): minutely: {"summary":"Partly cloudy for the hour.","icon":"partly-cloudy-night","data":....
D/JSON ( 1590): summary: Partly cloudy for the hour.
D/JSON ( 1590): data @ index0: {"time":1443331140,"precipIntensity":0,"precipProbability":0}
D/JSON ( 1590): data @ index1: {"time":1443331200,"precipIntensity":0,"precipProbability":0}
....
答案 2 :(得分:0)
这不是一个有效的json。 您可以检查logcat是否抛出异常。 如果已捕获异常,请尝试并打印堆栈跟踪。这将帮助您了解问题所在。
答案 3 :(得分:0)
我再次检查你的json,它应该是两种情况之一 第一个就像是
{
"latitude":37.8267,
"longitude":-122.423,
"timezone":"America/Los_Angeles",
"offset":-7,
"currently":{
"time":1443322196,
"summary":"Partly Cloudy",
"icon":"partly-cloudy-night",
"nearestStormDistance":13,
"nearestStormBearing":77,
"precipIntensity":0,
"precipProbability":0,
"temperature":63.94,
"apparentTemperature":63.94,
"dewPoint":55.46,
"humidity":0.74,
"windSpeed":8.59,
"windBearing":277,
"visibility":8.51,
"cloudCover":0.44,
"pressure":1010.39,
"ozone":261.48
},
"minutely":{
"summary":"Partly cloudy for the hour.",
"icon":"partly-cloudy-night",
"data":[
{
"time":1443322140,
"precipIntensity":0,
"precipProbability":0
},
}
并且解决方案看起来像。
JSONObject minutely = forecast.getJSONObject("minutely");
它喜欢
[
{
"latitude": 37.8267,
"longitude": -122.423,
"timezone": "America/Los_Angeles",
"offset": -7,
"currently": {
"time": 1443322196,
"summary": "Partly Cloudy",
"icon": "partly-cloudy-night",
"nearestStormDistance": 13,
"nearestStormBearing": 77,
"precipIntensity": 0,
"precipProbability": 0,
"temperature": 63.94,
"apparentTemperature": 63.94,
"dewPoint": 55.46,
"humidity": 0.74,
"windSpeed": 8.59,
"windBearing": 277,
"visibility": 8.51,
"cloudCover": 0.44,
"pressure": 1010.39,
"ozone": 261.48
},
"minutely": {
"summary": "Partly cloudy for the hour.",
"icon": "partly-cloudy-night",
"data": [
{
"time": 1443322140,
"precipIntensity": 0,
"precipProbability": 0
}
]
}
}
]
并且解决方案是您应该将预测对象更改为JsonArray。
您可以测试json here
的验证答案 4 :(得分:0)
分钟不是JSONArray它的JSONObject。 试试这个: -
JSONObject forecast = new JSONObject(jsonData);
JSONObject jsonObjMinutely= forecast.getJSONObject("minutely");
String summary = jsonObjMinutely.getString("summary");
答案 5 :(得分:0)
你的JSON真是无效..
让我们来看看那个更接近的人:
"minutely":{
"summary":"Partly cloudy for the hour.",
"icon":"partly-cloudy-night",
"data":[
{
"time":1443322140,
"precipIntensity":0,
"precipProbability":0
},
它没有微小的JSONObject和数据的JSONArray的结束括号。另外,它有一个逗号,其中“data”数组实际上包含一个元素。
将其修复为:
"minutely":{
"summary":"Partly cloudy for the hour.",
"icon":"partly-cloudy-night",
"data":[
{
"time":1443322140,
"precipIntensity":0,
"precipProbability":0
} ]
}
然后你的“微小”物体现在有效。这是第一次。
然后得到你的“微小”
...
JSONObject forecast = new JSONObject(jsonData);
JSONObject minutely = forecast.getJSONObject("minutely");
答案 6 :(得分:0)
Gson gson = new Gson();
YourModelClass object = gson.fromJson(jsonResponseObject.toString(), YourModelClass.class);
在这里你成功解析了json。现在是时候使用您的模型类来获取您想要的数据了
答案 7 :(得分:-1)
请编辑您的代码,如下所示
JSONObject currently = forecast.getJSONObject("currently");
Double humidity = currently.getDouble("humidity");
Long time = currently.getLong("time");
String icon = currently.getString("icon");
Double precipChance = currently.getDouble("precipProbability");
Double temp = currently.getDouble("temperature");
String timezone = forecast.getString("timezone");
JSONObject minutely = forecast.getJSONObject("minutely");
请查看以下链接,了解Json Parsing的完整示例
https://www.dropbox.com/s/q6cjifccbbw9nl1/JsonParsing.zip?dl=0