2015-09-27 92 views
1

在我的一個類的android項目中,我有一個JSONarray,其中有像這樣的值。從JSONarray獲取具體的值

{"imgUrl":"\\assets\\images\\projectpics\\normalt\\Edited_front.jpg","Name":"Normal T-shirt","View":"Unread","Status":"Cart","Quantity":"10","Date_Sub":"2015-09-26","Comment":null,"Customer_ID":"12","Order_ID":"21","Product_ID":"1","Date_Del":null} 

我需要分配給CUSTOMER_ID爲一個字符串值,

我該怎麼辦呢?

回答

0

如果您有名爲jsonArray的JsonArray。

for(int i=0; i<jsonArray.length(); i++) { 
    JSONObject item = jsonArray.getJSONObject(i); //gets the ith Json object of JSONArray 
    String customerId = item.getString("Customer_ID"); 
} 
+0

感謝,但增加一個嘗試捕捉需要 –

0
try { 
     String jsonString = 
       "{\"imgUrl\":\"\\\\assets\\\\images\\\\projectpics\\\\normalt\\\\Edited_front.jpg\",\"Name\":\"Normal T-shirt\",\"View\":\"Unread\",\"Status\":\"Cart\",\"Quantity\":\"10\",\"Date_Sub\":\"2015-09-26\",\"Comment\":null,\"Customer_ID\":\"12\",\"Order_ID\":\"21\",\"Product_ID\":\"1\",\"Date_Del\":null}\n"; 
     JSONObject jsonObject = new JSONObject(jsonString); 
     String customerID = jsonObject.getString("Customer_ID"); 
     Log.d("JSON", "Customer_ID: " + customerID); 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 

輸出:

D/JSON(1165): Customer_ID: 12