从json响应中将String转换为android中的数组

时间:2016-08-03 10:17:40

标签: java android arrays string type-conversion

我正在以这样的数组形式获得json响应

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

,但它没有在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"}] 

这是我的回复,我在hashmap中存储了daysofInterest ...并且正在使用hashmap

但我希望以数组形式获得

3 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

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

Here是Gson库,here是示例。

答案 2 :(得分:0)

希望这很有用。

yourstring["AA", "BB", "CC"]

if (!yourstring.equals("")) {
    String[] type = yourstring
    .replace("[", "")
    .replace("]", "")
    .replace("\"", "")
    .split(",");
}