2016-10-05 5 views
-3

다음 json 데이터가 있는데 목록보기로 표시하려고하지만 다음 오류가 나타납니다.jsonarray를 json 개체로 변환하는 방법

[ 
{ 
"sendingReqDataSet": { 
"totalCount": 1, 
"limit": 50, 
"offset": 0, 
"status_code": true, 
"contacts": [ 
{ 
"id": 1, 
"request_to": "Shehla_kiran", 
"request_by": "ayesha", 
"product_name": "Dead Space", 
"schedule_date_time": "2016-09-21 00:00:00", 
"place": "lhr", 
"request_type": "Swap", 
"status": "Pending" 
}, 
{ 
"id": 2, 
"request_to": "muqadas", 
"request_by": "muqadas", 
"product_name": "battleField", 
"schedule_date_time": "2016-09-22 00:00:00", 
"place": "lhr", 
"request_type": "Swap", 
"status": "Pending" 
}, 
{ 
"id": 3, 
"request_to": "Shehla_kiran", 
"request_by": "Shehla_kiran", 
"product_name": "Mario", 
"schedule_date_time": "2016-09-12 00:00:00", 
"place": "Lahore", 
"request_type": "Swap", 
"status": "Pending" 
}, 
{ 
"id": 4, 
"request_to": "Shehla_kiran", 
"request_by": "Mari", 
"product_name": "Mario", 
"schedule_date_time": "0000-00-00 00:00:00", 
"place": "", 
"request_type": "Swap", 
"status": "Pending" 
}, 
{ 
"id": 5, 
"request_to": "Shehla_kiran", 
"request_by": "Faisal Ali", 
"product_name": "Dead Space", 
"schedule_date_time": "2016-10-28 00:00:00", 
"place": "lahore", 
"request_type": "Swap", 
"status": "Pending" 
}, 
{ 
"id": 6, 
"request_to": "muqadas", 
"request_by": "Faisal Ali", 
"product_name": "battleField", 
"schedule_date_time": "2016-10-25 00:00:00", 
"place": "Canal", 
"request_type": "Swap", 
"status": "Pending" 
}, 
{ 
"id": 7, 
"request_to": "Shehla_kiran", 
"request_by": "Shehla_kiran", 
"product_name": "Dead Space", 
"schedule_date_time": "2016-10-17 00:00:00", 
"place": "Lahore", 
"request_type": "Swap", 
"status": "Pending" 
} 
] 
} 
} 
] 

내가 구문 분석하려고 시도하고 목록보기에 표시하면 다음 오류가 발생합니다.

W/System.err: org.json.JSONException: Value [{"sendingReqDataSet":{"totalCount":1,"limit":50,"offset":0,"status_code":true,"contacts":[{"id":1,"request_to":"Shehla_kiran","request_by":"ayesha","product_name":"Dead Space","schedule_date_time":"2016-09-21 00:00:00","place":"lhr","request_type":"Swap","status":"Pending"},{"id":2,"request_to":"muqadas","request_by":"muqadas","product_name":"battleField","schedule_date_time":"2016-09-22 00:00:00","place":"lhr","request_type":"Swap","status":"Pending"},{"id":3,"request_to":"Shehla_kiran","request_by":"Shehla_kiran","product_name":"Mario","schedule_date_time":"2016-09-12 00:00:00","place":"Lahore","request_type":"Swap","status":"Pending"},{"id":4,"request_to":"Shehla_kiran","request_by":"Mari","product_name":"Mario","schedule_date_time":"0000-00-00 00:00:00","place":"","request_type":"Swap","status":"Pending"},{"id":5,"request_to":"Shehla_kiran","request_by":"Faisal Ali","product_name":"Dead Space","schedule_date_time":"2016-10-28 00:00:00","place":"lahore","request_type":"Swap","status":"Pending"},{"id":6,"request_to":"muqadas","request_by":"Faisal Ali","product_name":"battleField","schedule_date_time":"2016-10-25 00:00:00","place":"Canal","request_type":"Swap","status":"Pending"},{"id":7,"request_to":"Shehla_kiran","request_by":"Shehla_kiran","product_name":"Dead Space","schedule_date_time":"2016-10-17 00:00:00","place":"Lahore","request_type":"Swap","status":"Pending"}]}}] of type org.json.JSONArray cannot be converted to JSONObject 

현재이 작업을 수행 중입니다.

try { 

//   jsonObject=new JSONObject(); 
      JSONObject jsonObject = new JSONObject(json_string); 
//   JSONArray query = jsonParse.getJSONArray("courses"); 

      jsonArray=jsonObject.getJSONArray("contacts"); 


      int count=0; 
      String name,email,moblile; 

      while (count<jsonArray.length()){ 
       JSONObject JO=jsonArray.getJSONObject(count); 
       name=JO.getString("request_to"); 
       email= JO.getString("request_by"); 
       moblile=JO.getString("product_name"); 
       Contacts contacts=new Contacts(name,email,moblile); 
       contactAdapter.add(contacts); 
       count++; 
      } 


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

    } 
+0

가능한 경우 처음 및 마지막 대괄호를 바꿉니다. 그런 다음 json 객체가됩니다. –

답변

0
String data = /*load the json data*/ 
     try { 
      JSONArray jsonArray = new JSONArray(data); 
      JSONObject object = jsonArray.getJSONObject(0); 
      JSONObject sendingReqDataSetObject = object.getJSONObject("sendingReqDa`enter code here`taSet"); 
      JSONArray arrayContacts = sendingReqDataSetObject.getJSONArray("contacts"); 
      for(int i = 0; i<arrayContacts.length();i++){ 
       JSONObject contactObject = arrayContacts.getJSONObject(i); 
       System.out.println(contactObject.getString("status")); 
      } 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
0
JSOnArray array=new JSONArray(json_string); 

JSONObject obj=array.getObjetc("sendingReqDataSet"); 

jsonArray=obj.getJSONArray("contacts"); 
+0

이 코드 단편은 질문에 대답 할 수 있지만 방법이나 이유를 설명하기위한 컨텍스트는 제공하지 않습니다. 답을 설명하기 위해 한 두 문장을 더하는 것을 고려하십시오. – brandonscript

1

당신은 다음과 같은 코드를 사용하여 객체를 얻을 수 있습니다 :

jsonArray = jsonObject.getJSONArray("contacts"); 

for(int i=0; i<jsonArray.size(); i++) { 
    JSONObject jobject = jsonArray.getJSONObject(i) 
} 
0

사용 GSON 라이브러리 JSON으로 또는 JSON으로 변환 (https://github.com/google/gson)

도서관 지원 객체하기를.

+0

답변 해 주셔서 감사합니다. 해결되었습니다. – waleed

관련 문제