2016-12-10 9 views
0

AndroidスタジオでJSON APIを使用する際に問題が発生しています 多くの例について見ていますが、必要なデータを抽出する方法がまだわかりません以下の内容AndroidスタジオのJson解析の問題

JSONObject subObj = jsonObject.getJSONObject("response"); 

{ 
    "hereNow": { 
    "count": 1, 
    "items": [ 
     { 
     "id": "xxxxxxxxxx", 
     "createdAt": xxxxxxxxxx, 
     "type": "checkin", 
     "timeZoneOffset": 60, 
     "user": { 
      "id": "xxxxxxx", 
      "firstName": "xxxxxxxx", 
      "lastName": "xxxxxxxxxx", 
      "gender": "male", 
      "relationship": "friend", 
      "photo": { 
      "prefix": "xxxxxxxxx", 
      "suffix": "xxxxxxxxxx" 
      } 
     }, 
     "likes": { 
      "count": 0, 
      "groups": [ 

     ] 
    }, 
    "like": false 
     } 
    ] 
    } 
} 

のTh ...

は、いくつかの時間後、私は最終的にポイントを得ているが、私は抽出するために考え出しました現在の問題は次のとおりです。ユーザーオブジェクトの抽出方法私は

JSONObject 

JSONArray 

で多くのことをしようとしてきた。しかし、私はこれを取得し続ける:

Attempt to invoke virtual method 'org.json.JSONArray org.json.JSONObject.getJSONArray(java.lang.String)' on a null object reference 

誰かが私を助けてくださいことはできますか? 一般的に私はJSONを理解しているので、私は別のトピックにリダイレクトしないでください。この特定のケースではありません。

ありがとうございます。

+0

例を試してみて、[ここでの説明](http://stackoverflow.com/questions/5015844/parsing-json-object-in-java/40828056#を読むに動作するはずです40828056)、読んだ後に疑問がある場合は質問を更新してください –

答えて

0

これはあなたのため

try{ 
     JSONArray array = subObj.getJSONObject("hereNow").getJSONArray("items"); 
     for(int index=0; index<array.length();index++){ 
      JSONObject itemsObj = array.getJSONObject(index); 
      JSONObject userObj = itemsObj.getJSONObject("user"); 
      Log.d("Test", "Relationship "+userObj.get("relationship")); 
     } 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 
+0

この作品はありましたか?もしそうなら、他の人も同じような問題を抱えていることを知っているように。 –