2013-08-22 5 views
0

Когда я вернусь строки из другой деятельности и попытаться преобразовать его в JSONArray, программа показывает мне это:Значение типа org.json.JSONObject не могут быть преобразованы в JSONArray

08-22 14:51:35.313: E/YourCartActivity(1134): Error parsing data 
org.json.JSONException: 
Value {"yourCart_totalPrice":"5500.00","yourCart_productName":"AAA", 
"yourCart_productImg":"http:\/\/10.0.2.2\/appserv\/products\/aaa02.jpg", 
"yourCart_productID":"0000002", 
"yourCart_productAmount":"2", 
"yourCart_shopID":"001"} 
of type org.json.JSONObject cannot be converted to JSONArray 

ли кто-нибудь знает, как решить Эта проблема?

Это мой код:

String json = yourCartConnect.getYourCartDetails(); 

     try { 
      JSONArray jResult = new JSONArray(json); 


      for(int i=0;i<json.length();i++){ 
       JSONObject f = jResult.getJSONObject(i); 
       YourCartEntry resultRow = new YourCartEntry(); 

       resultRow.setYourCart_shopID(f.getString("yourCart_shopID")); 
       resultRow.setYourCart_totalPrice(f.getString("yourCart_totalPrice")); 
       resultRow.setYourCart_productName(f.getString("yourCart_productName"));  
       resultRow.setYourCart_productID(f.getString("yourCart_productID")); 
       resultRow.setYourCart_productImg(f.getString("yourCart_productImg")); 
       resultRow.setYourCart_productAmount(f.getString("yourCart_productAmount")); 

       yourCartEntries.add(resultRow); 
      } 

     setList(yourCartEntries); 


     } catch (JSONException e) { 
      Log.e("YourCartActivity", "Error parsing data " + e.toString()); 
      errorConnectToServer(); 
     } 
+4

ваш JSON является объектом JSON, а не массив JSON ... – njzk2

+0

Вы используете PHP, чтобы построить строку ответа JSON ? – Secko

+0

Опубликовать полный код, пожалуйста! –

ответ

0

Попробуйте этот код:

String json = yourCartConnect.getYourCartDetails(); 



     try { 
       JSONObject jsonObject = new JSONObject(json) 
       JSONArray jResult = jsonObject.getJSONArray("Your array key"); 

       for(int i=0;i<jResult.length();i++){ 
        JSONObject f = jResult.getJSONObject(i); 
        // write your remaining code here 
       } 

      setList(yourCartEntries); 


      } catch (JSONException e) { 
       Log.e("YourCartActivity", "Error parsing data " + e.toString()); 
       errorConnectToServer(); 
      } 
0

Попробуйте этот код ..

try { 
    JSONArray jArray = new JSONArray("your JSON result........"); 
    JSONObject json_data = null; 
    StringBuilder print = new StringBuilder(); 
    for (int i = 0; i < jArray.length(); i++) { 
     json_data = jArray.getJSONObject(i); 
     System.out.println("json_data : " + json_data); 
     System.out.println("\n"); 
     print.append(json_data.getString("yourCart_shopID") + ", " + 
      json_data.getString("yourCart_totalPrice") + ", " + 
      json_data.getString("yourCart_productName") + ", " + 
      json_data.getString("yourCart_productID") + ", " + 
      json_data.getString("yourCart_productImg") + ", " + 
      json_data.getString("yourCart_productAmount") + "\n"); 
     yourCartEntries.add(print.toString()); 
    } 
    setList(yourCartEntries); 
} catch (JSONException e1) { 
    e1.printStackTrace(); 
} 
0

Вы JSONObject String, не JSONArray,

JSONArray here

EG:

[{"yourCart_totalPrice":"5500.00","yourCart_productName":"AAA","yourCart_productImg":"http:\/\/10.0.2.2\/appserv\/products\/aaa02.jpg","yourCart_productID":"0000002","yourCart_productAmount":"2","yourCart_shopID":"001"}] 
0

Попробуйте это ..

String json = yourCartConnect.getYourCartDetails(); 

     try { 
      if(!TextUtils.isEmpty(json)){ 
       JSONObject f=new JSONObject(json); 
       YourCartEntry resultRow = new YourCartEntry();  
       resultRow.setYourCart_shopID(f.getString("yourCart_shopID")); 
       resultRow.setYourCart_totalPrice(f.getString("yourCart_totalPrice")); 
       resultRow.setYourCart_productName(f.getString("yourCart_productName"));  
       resultRow.setYourCart_productID(f.getString("yourCart_productID")); 
       resultRow.setYourCart_productImg(f.getString("yourCart_productImg")); 
       resultRow.setYourCart_productAmount(f.getString("yourCart_productAmount")); 
       setList(resultRow); 
      } 
     } catch (JSONException e) { 
      Log.e("YourCartActivity", "Error parsing data " + e.toString()); 
      errorConnectToServer(); 
     } 
Смежные вопросы