我刚接触JSON,但已经成功地从其他几个JSON请求中提取了数据。这个给我带来了麻烦。任何帮助或指示都将不胜感激。
这是JSON请求:http://api.wunderground.com/api/95e20de6002dc6f0/currenthurricane/view.json
这上面拉下了我想要的数据。
下面是我遇到问题的代码:
public static ArrayList<CycloneData> extractFeatureFromJson(String cycloneJSON) {
// Create an empty ArrayList to start adding Cyclones to
ArrayList<CycloneData> cyclones = new ArrayList<>();
// try to parse the cycloneJSON response string. If there's a problem with the way the JSON
// is formatted, a JSONException exception object will be thrown.
// Catch the exception, and print the error message to the logs.
try {
JSONObject rootJsonObject = new JSONObject(cycloneJSON);
// Create JSONArray associated with the key called "currenthurricane", which represents
// a list of cyclones from JSON response.
JSONArray currentHurricaneArray = rootJsonObject.getJSONArray("currenthurricane");
//Loop through each section in the currentHurricaneArray array & create an
//{@link CycloneData} object for each one
for (int i = 0; i < currentHurricaneArray.length(); i++) {
//Get cyclone JSONObject at position i in the array
JSONObject cycloneProperties = currentHurricaneArray.getJSONObject(i);
//Extract “stormName_Nice” for Cyclone's name
String name = cycloneProperties.optString("stormName_Nice");
// Extract the value for the key called "url"
String url = cycloneProperties.optString("url");
int category = cycloneProperties.optInt("SaffirSimpsonCategory");
CycloneData cyclone = new CycloneData(category, name, url);
//Add new cyclone to list
cyclones.add(cyclone);
}
} catch (JSONException e) {
// If an error is thrown when executing any of the above statements in the "try" block,
// catch the exception here, so the app doesn't crash. Print a log message
// with the message from the exception.
Log.e("Utils", "Problem parsing the cyclone JSON results", e);
}
// Return the list of cyclones
return cyclones;
}使用Android Studio中的调试器,我可以在以下位置看到该currentHurricaneArray:JSONArray currentHurricaneArray = rootJsonObject.getJSONArray("currenthurricane");
正在获取预期的JSON数组数据。
当for循环启动JSONObject:JSONObject cycloneProperties = currentHurricaneArray.getJSONObject(i);时
也有我要找的正确的数组信息。
然而,在那之后,当它开始提取字符串时。String name = cycloneProperties.optString("stormName_Nice");
它不返回任何内容。
调试显示:name = ""
如果我使用JSON查询工具,我可以获得我想要的信息,但我不知道如何在我的代码中让它工作。
我确信我的字符串提取是错误的,我只是想不出如何使其正确。也许我错了,他们都错了。
*
好的,Gaëtan Maisse让我开始了。下面是我为了让它工作而做的事情。
for (int i = 0; i < currentHurricaneArray.length(); i++) {
//Get cyclone JSONObject at position i in the array
JSONObject cycloneProperties = currentHurricaneArray.getJSONObject(i);
// Extract "stormInfo" object
JSONObject stormInfo = cycloneProperties.optJSONObject("stormInfo");
//Extract “stormName_Nice” & "requesturl" for Cyclone's name and url
String name = stormInfo.optString("stormName_Nice");
String url = stormInfo.optString("requesturl");
// Extract "Current" object
JSONObject Current = cycloneProperties.optJSONObject("Current");
// Extract "SaffirSimpsonCategory" key
int category = Current.optInt("SaffirSimpsonCategory");
CycloneData cyclone = new CycloneData(category, name, url);
//Add new cyclone to list
cyclones.add(cyclone);
}发布于 2017-03-05 16:31:07
您在解析stormName_Nice的过程中缺少JSON密钥stormInfo:
{
"response": {
"version": "0.1",
"termsofService": "http://www.wunderground.com/weather/api/d/terms.html",
"features": {
"currenthurricane": 1
}
},
"currenthurricane": [{
"stormInfo": {
"stormName": "Daniel",
"stormName_Nice": "Hurricane Daniel",
"stormNumber": "ep201204"
},
...,
"SaffirSimpsonCategory": 1,
"url":"URL",
...
}]
}它应该更好地与以下内容一起工作:
JSONObject rootJsonObject = new JSONObject(cycloneJSON);
// Create JSONArray associated with the key called "currenthurricane", which represents
// a list of cyclones from JSON response.
JSONArray currentHurricaneArray = rootJsonObject.getJSONArray("currenthurricane");
//Loop through each section in the currentHurricaneArray array & create an
//{@link CycloneData} object for each one
for (int i = 0; i < currentHurricaneArray.length(); i++) {
//Get cyclone JSONObject at position i in the array
JSONObject cycloneProperties = currentHurricaneArray.getJSONObject(i);
// Extract "stormInfo" object
JSONObject stormInfo = cycloneProperties.getJSONObject("stormInfo");
//Extract “stormName_Nice” for Cyclone's name
String name = stormInfo.optString("stormName_Nice");
// Extract other values from cycloneProperties
String url = cycloneProperties.optString("url");
int category = cycloneProperties.optInt("SaffirSimpsonCategory");
CycloneData cyclone = new CycloneData(category, name, url);
//Add new cyclone to list
cyclones.add(cyclone);
}发布于 2017-03-05 09:48:48
optString(String name,String fallback),返回按名称映射的值(如果存在),如有必要则强制执行;如果不存在此类映射,则返回回退。print fallback to test.if fallback被触发,则没有key.This提示您的json结构格式错误,或者您的解析逻辑不适合您正在使用的已定义结构。
https://stackoverflow.com/questions/42603367
复制相似问题