Java JsonObject JsonArray解析问题
我是JSON新手,但已经成功地从其他几个JSON请求中提取数据。这个给我带来了麻烦。任何帮助或指点都将不胜感激 这是JSON请求: 上面提到的数据是我想要的 以下是我遇到问题的代码:Java JsonObject JsonArray解析问题,java,android,json,Java,Android,Json,我是JSON新手,但已经成功地从其他几个JSON请求中提取数据。这个给我带来了麻烦。任何帮助或指点都将不胜感激 这是JSON请求: 上面提到的数据是我想要的 以下是我遇到问题的代码: public static ArrayList<CycloneData> extractFeatureFromJson(String cycloneJSON) { // Create an empty ArrayList to start adding Cyclones to Arra
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;
}
optString(字符串名称,字符串回退),返回按名称映射的值(如果存在),必要时强制该值,如果不存在此类映射,则返回回退。打印回退以进行测试。如果触发了回退,则没有键。这表明您的json结构格式不正确,或者您的解析逻辑不适合所使用的定义结构。您在
stormName\u 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);
}
JSONObject rootJsonObject=newjsonobject(cycloneJSON);
//创建与名为“currenthurricane”的键关联的JSONArray,该键表示
//JSON响应中的气旋列表。
JSONArray currentHurricaneArray=rootJsonObject.getJSONArray(“currenthurricane”);
//循环浏览currentHurricaneArray中的每个部分并创建一个
//每个对象的{@link CycloneData}对象
对于(int i=0;i
此时似乎“String name=cycloneProperties.optString(“stormName_Nice”);”键不存在,因此它返回null。你确定这是正确的密钥吗?请添加你的JSONY添加你的json实际的一个。(显然)你提供给我们的json链接不起作用。是的,两个不同的结构,这就是了。让我走上正轨。我误解了我是如何拿到钥匙的。向原始帖子添加工作代码。
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);
}