我是JSON的新手,但已成功从其他JSON请求中提取数据。这一个给我带来麻烦。任何帮助或指针,将不胜感激。JsonObject JsonArray解析问题
这是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查询工具的信息,但我无法弄清楚如何得到它在我的代码工作。
我敢肯定我的字符串提取是错误的,我只是无法弄清楚如何使它正确。或者也许我错了。
*************良好的代码下面*******************
确定的Gaetan 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);
}
似乎在这一点上“字符串名称= cycloneProperties.optString (“stormName_Nice”); “密钥不存在,因此为什么它返回null。你确定这是正确的钥匙? –
请添加您的JSON –
是添加您的json实际的。 – Remario