2017-03-05 13 views
0

私は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); 
     } 
+0

、それはこの時点で「文字列名= cycloneProperties.optStringようです( "stormName_Nice"); "キーが存在しないため、なぜnullを返すのですか?それが正しいキーだと確信していますか? –

+2

JSONを追加してください –

+0

はい、実際のJSONを追加してください。 – Remario

答えて

0

{ 
    "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); 
} 
+1

これはそれです。そして、正しい道で私を得ました。私はキーズにどうやって行かれたのか誤解しました。元の投稿に作業コードを追加する –

0

optstringに(文字列名、文字列のフォールバック)は、必要に応じて強制する、それが存在する場合は名前によってマップされた値を返し、またはそのようなマッピングが存在しない場合のフォールバック。あなたが使用している定義された構造に対して、あなたのjson構造が誤っているか、解析ロジックが不適切であることを示唆しています。あなたはstormName_Niceのためのあなたの構文解析過程でJSONのキーstormInfoが欠落している