私はJSONを初めて使っていますが、他のJSONリクエストからデータを取得することに成功しました。これは私にトラブルを与えている。どのような助けや指針をいただければ幸いです。 http://api.wunderground.com/api/95e20de6002dc6f0/currenthurricane/view.jsonJsonObject JsonArray解析の問題
上記の私が望んでいたデータを引き下げる:
これはJSON要求です。
以下は私がトラブルを抱えています私のコードです:JSONArray currentHurricaneArray = rootJsonObject.getJSONArray("currenthurricane");
が予想される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がでていることがわかります。
は、forループJSONObjectを開始すると:JSONObject cycloneProperties = currentHurricaneArray.getJSONObject(i);
は、私も探しています正しい配列情報を持っています。
しかし、それ以降は文字列の抽出を開始します。 String name = cycloneProperties.optString("stormName_Nice");
何も返されません。
デバッグを示しています。name = ""
私はJSONクエリツールを使用している場合、私が欲しい情報を得ることができるが、私はそれは私のコードでの作業を取得する方法を見つけ出すことはできません。
文字列の抽出が間違っていると確信していますが、正しい方法を理解できません。あるいは、私が間違っているのかもしれない。
*******************
[OK]をガエタン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