1

I am getting json response in array form like this

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

, but it is not saving as a array in android, I am storing that in a string like this

 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"}] 

this is my response and I am storing that daysofInterest in hashmap...and getting using hashmap

But I want to get that in a array form

Sanoop Surendran
  • 3,484
  • 4
  • 28
  • 49
ajay110125
  • 81
  • 2
  • 5
  • What is the exact response and how are you trying to store it in an array? Post the code that you tried. – Prerak Sola Aug 03 '16 at 10:19
  • [{"careTypeId":"10","careTypeName":"Vacation Care","daysOfinterest":["Tuesday","Thursday","Saturday"],"childDaysOfInterestId":"424"},{"careTypeId":"10","careTypeName":"Vacation Care","daysOfinterest":["Monday","Wednesday","Friday"],"childDaysOfInterestId":"425"}] this is my response and I am storing that daysofInterest in hashmap...and getting using hashmap – ajay110125 Aug 03 '16 at 10:21

3 Answers3

1

you can convert the json array into a java array with this

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()]);

then you can pass the array elements to your map. more info here Convert Json Array to normal Java Array

Community
  • 1
  • 1
El0din
  • 3,208
  • 3
  • 20
  • 31
0
GsonBuilder gsonBuilder = new GsonBuilder();
Gson gson = gsonBuilder.create();
Days[] daysArray = gson.fromJson(jsonString, Days[].class);

Here is Gson library and here is example.

faranjit
  • 1,567
  • 1
  • 15
  • 22
0

Hope this is useful.

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

if (!yourstring.equals("")) {
    String[] type = yourstring
    .replace("[", "")
    .replace("]", "")
    .replace("\"", "")
    .split(",");
}
Pingolin
  • 3,161
  • 6
  • 25
  • 40
Infomaster
  • 793
  • 7
  • 8