2016-08-03 165 views
0

我得到的數組的形式這樣字符串轉換爲從JSON響應

["Monday","Wednesday","Friday"] 

JSON響應,但它不保存在Android的一個數組中的Android陣,我存儲在像這樣的字符串

String daysOfInterest = map.get("daysOfinterest"); 

[{"careTypeId":"10","careTypeName":"Vacation Care","daysOfinterest":["Tuesday","Thursday","Saturday"],"childDaysOfInterestId"‌​:"424"}, 
    {"careTypeId":"10","careTypeName":"Vacation Care","daysOfinterest":["Monday","Wednesday","Friday"],"childDaysOfInterestId":"‌​425"}] 

這是我的反應,我認爲存儲在daysofInterest HashMap中......並使用HashMap的

越來越但是我想,在一個陣列形式

+0

什麼是確切的響應,你如何試圖將其存儲在數組中?發佈您嘗試的代碼。 –

+0

[{「careTypeId」:「10」,「careTypeName」:「Vacation Care」,「daysOfinterest」:[「Tuesday」,「Thursday」,「Saturday」],「childDaysOfInterestId」:「424」},{「careTypeId 「:」10「,」careTypeName「:」Vacation Care「,」daysOfinterest「:[」Monday「,」Wednesday「,」Friday「],」childDaysOfInterestId「:」425「}]這是我的迴應,那天daysofInterest在hashmap中......並且正在使用hashmap – ajay110125

回答

1

可以JSON數組轉換爲java數組與此

ArrayList<String> list = new ArrayList<String>();  
JSONArray jsonArray = (JSONArray)jsonObject; 
if (jsonArray != null) { 
    int len = jsonArray.length(); 
    for (int i=0;i<len;i++){ 
    list.add(jsonArray.get(i).toString()); 
    } 
} 
String[] myArray = list.toArray(new String[list.size()]); 

那麼你可以傳遞數組元素地圖。這裏Convert Json Array to normal Java Array

0
GsonBuilder gsonBuilder = new GsonBuilder(); 
Gson gson = gsonBuilder.create(); 
Days[] daysArray = gson.fromJson(jsonString, Days[].class); 

Here更多信息是GSON圖書館和here是例子。