2017-04-20 38 views
0

所有數組元素如何從JSON所有數組元素拆分在Java中分裂從JSON在Java

{ 
    "id": "405844", 
    "username": "abc8", 
    "password_hash": "dkjfhs7rjkds932dajk2900932", 
    "role": "customer", 
    "followed_stores" : 
    [ 
    { 
     "id": "sda", 
     "created": { 
       "date": "2016-06-28 14: 43 : 28", 
       "epoch": 1467125008563 
     } 
    } 
    ], 
    "followed_influencers": [ 
    { 
     "id": "sda", 
     "created": { 
       "date": " 2016-06-28 14: 43 : 28", 
       "epoch": 1467125008563 
     } 
    } 
    ] 
} 

我想3 JSON在2 JSON我必須followed_stores陣列,followed_influencer陣列和在最後剩下的JSON

回答

0

你可以用服務器響應創建org.json.JSONArray的一個實例:

String serverResponse = "YOUR STRING"; 
JSONArray serverJsonArray = new JSONArray(serverResponse); 

然後填寫產品列表:

ArrayList<String> products = new ArrayList<>(serverJsonArray.length()); 
for(int i = 0; i < serverJsonArray.length(); i++){ 
    products.add(serverJsonArray.getString(i)); 
} 

或者,如果你絕對要一個String數組:

String[] products = new String[serverJsonArray.length()]; 
for(int i = 0; i < serverJsonArray.length(); i++){ 
    products[i] = serverJsonArray.getString(i); 
}