Reputation: 153
I want to split the following:
This is the JSON array I'm getting
[
"Bus Stand",
"Vazicherry",
"Shavakottapalam",
"Arattuvazhy",
"Kallappura",
"Kommady",
"Thumpoly",
"Poonkavu",
"Pathirappally",
"Valiya Kalavoor",
"KSDP",
"Bernad Junction",
"Kalavoor"
]
I want to parse it as java array which includes something like this Bus Stand, Vazicherry, Shavakottapalam etc...
I tried it using simple split method in java, How can i split it using JSON object?
String products[] = x.getStops().split("[\"\\],\\[\"] ");
Upvotes: 1
Views: 147
Reputation: 12347
You can create an instance of org.json.JSONArray
with server response:
String serverResponse = "YOUR STRING";
JSONArray serverJsonArray = new JSONArray(serverResponse);
And then fill products list:
ArrayList<String> products = new ArrayList<>(serverJsonArray.length());
for(int i = 0; i < serverJsonArray.length(); i++){
products.add(serverJsonArray.getString(i));
}
Or if you absolutely want a String array:
String[] products = new String[serverJsonArray.length()];
for(int i = 0; i < serverJsonArray.length(); i++){
products[i] = serverJsonArray.getString(i);
}
Upvotes: 2
Reputation: 469
Looks like a JSONArray? I suggest using the json objects to get a string array out of this and then do with it whatever you want. There are some ways to do that and there are a lot of examples out there.
Upvotes: -1