2016-10-11 11 views
-3

私はvolleyを使用し、json解析を実装しています。私はjsonの解析を行い、リストビューでデータを表示しています。応答時にJSONを解析する方法

{ 
"result": [{ 
    "id": "1", 
    "name": "Prabhat", 
    "email": "[email protected]" 
}, { 
    "id": "2", 
    "name": "Apurva", 
    "email": "[email protected]" 
}, { 
    "id": "3", 
    "name": "sunny", 
    "email": "[email protected]" 
}, { 
    "id": "4", 
    "name": "Creation InfoTech", 
    "email": "[email protected]" 
}, { 
    "id": "5", 
    "name": "Sanjay Mishra", 
    "email": "[email protected]" 
}] 

}

そして、私のJavaコードは

での解析方法を、私はすべてのもの(ゲッターセッター、シングルトンクラス、アダプタなど)を作っていたが、解析の時点で私が困難に直面しています
JsonObjectRequest request = new JsonObjectRequest(url_Array, new JSONObject(), new Response.Listener<JSONObject>() { 
     @Override 
     public void onResponse(JSONObject response) { 

      for (int i = 0; i < response.length(); i++) { 
       try { 
        Log.d(TAG, "working"); 
        JSONObject object = response.getJSONObject(i); 
        // Now this is my PersonInfo class in which i have define my getters and setters 
        PersonInfo info = new PersonInfo(object.getString("id"), object.getString("name"), object.getString("email")); 
        personInfos.add(info); 

       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 

      } 
      adapter.notifyDataSetChanged(); 

     } 
    }, new Response.ErrorListener() { 
     @Override 
     public void onErrorResponse(VolleyError error) { 
      Toast.makeText(getApplicationContext(), "Something not ok", Toast.LENGTH_SHORT).show(); 

     } 

    }); 
    AppController.getInstance(getApplicationContext()).AddtoRequestQueue(request); 

} 

**解析の難しさに直面し、親切にする前に**

+5

"解析に直面するのが難しい"ということができます。私たちはこれをどうするのですか?どういう意味ですか?エラーが出ていますか? – rmlan

+0

はい、私は応答時にエラーに直面しています。 –

+0

あなたはどのようなエラーを受け取りますか? –

答えて

1

返信リストを歩いて、あなたはJSONArrayを取得する最初のものが必要です。

JSONArray array = response.getJSONArray("result")

次に、あなたが配列歩く:

for (int i = 0; i < array.length(); i++) {...}

+0

それは働いていません –

0

このコードは、2つの文字列の配列にデータを読み込むのに役立ちます試してみてください

JsonObjectRequest request = new JsonObjectRequest(url_Array, new JSONObject(), new Response.Listener<JSONObject>() { 
     @Override 
     public void onResponse(JSONObject response) { 
     JSONArray jsonArray = jsnobject.getJSONArray("result"); 
     String[] name=new String[jsonArray.length()]; 
     String[] email=new String[jsonArray.length()]; 
      for (int i = 0; i < jsonArray.length(); i++) { 
       try { 
        Log.d(TAG, "working"); 
        JSONObject student=jsonArray.getJSONObject(i); 
        name[i]=student.getString("name"); 
        email[i]=student.getString("email"); 
       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 
      } 
      adapter.notifyDataSetChanged(); 
     } 
    }, new Response.ErrorListener() { 
     @Override 
     public void onErrorResponse(VolleyError error) { 
      Toast.makeText(getApplicationContext(), "Something not ok", Toast.LENGTH_SHORT).show(); 
     } 
    }); AppController.getInstance(getApplicationContext()).AddtoRequestQueue(request); 

} 
+0

それはリストビューの最初の行にのみデータを表示しています、残りのデータ(4行)は表示されません –

関連する問題