2016-10-11 2 views
-3

저는 발리 (ballley)를 사용하고 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]" 
}] 

}

그리고 내 자바 코드가

입니다 구문 분석하는 방법을 어려움에 직면하고있다
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 :

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

+0

작동하지 않습니다 –

0

것은이 코드는 두 개의 문자열 배열로 데이터를 읽는 데 도움이됩니다보십시오 :

JSONArray array = response.getJSONArray("result")

는 그런 다음 배열을 걸어

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 행)가 표시되지 않고 목록보기의 첫 번째 행에만 데이터가 표시됩니다. –

관련 문제