2016-02-22 88 views
1

我在JSONArray ary2中有這個;從JSON數組中解析字符串數組

JSON字符串是:

[ 
     { 
     "item_id": "1", 
     "Head_item_id": "1", 
     "major_item_id": "1", 
     "Quantity": "10", 
     "selling_prize": "20", 
     "MRP": "90", 
     "title": "JK Lakshmi PPC Cement", 
     "SKU": "B2WBUICEM2" 
     }, 
     { 
     "item_id": "2", 
     "Head_item_id": "1", 
     "major_item_id": "1", 
     "Quantity": "10", 
     "selling_prize": "30", 
     "MRP": "80", 
     "title": "JK Lakshmi PPC Cement", 
     "SKU": "B2WBUICEM2" 
     }, 
     { 
     "item_id": "3", 
     "Head_item_id": "1", 
     "major_item_id": "1", 
     "Quantity": "10", 
     "selling_prize": "10", 
     "MRP": "70", 
     "title": "Shree Ultra OPC 43 Grade Cement", 
     "SKU": "B2WBUICEM5" 
     } 
     ] 

我想String數組爲每個屬性,如:

String[] item_id; 
String[] Head_item_id; 

等 無法得到它。請幫忙。

回答

1

試試這個:

JSONArray arr = new JSONArray(yourJSONresponse); 
String[] item_id=new String[array.length()]; 
String[] Head_item_id=new String[arr .length()]; 
for(int i = 0; i < arr.length(); i++){ 
    item_id[i]=arr.getJSONObject(i).getString("item_id"); 
    Head_item_id[i]=arr.getJSONObject(i).getString("Head_item_id"); 
} 
1

您需要按照下列步驟操作: - >您的JSON數據和provite getter和setter每個屬性創建POJO。使用GSON解析您的JSON數據 - >獲取所需的屬性(使用getters)並將其存儲在相應的字符串數組中

+0

嘗試使用任何使用gson的json解析教程 – Pallav