0

I want to fetch a text (some urls) from Json and add them straightly to a String[] in android. The text is like

"http://www.androiddrawer.com/wp-content/uploads/2013/05/Subway-Surfers-screenshot-4-1.png","http://www.androiddrawer.com/wp-content/uploads/2013/05/Subway-Surfers-screenshot-4-2.png","http://www.androiddrawer.com/wp-content/uploads/2013/05/Subway-Surfers-screenshot-4-3.png","http://www.androiddrawer.com/wp-content/uploads/2013/05/Subway-Surfers-screenshot-4-4.png"

I want to put this text into a String[]. I know I can use this method:

products = json.getJSONArray(TAG_PRODUCTS);
JSONObject c = products.getJSONObject(0);
String name = c.getString(TAG_NAME);

But it gives me String not String[].

2 Answers 2

3

By looking your String

"http://www.androiddrawer.com/wp-content/uploads/2013/05/Subway-Surfers-screenshot-4-1.png","http://www.androiddrawer.com/wp-content/uploads/2013/05/Subway-Surfers-screenshot-4-2.png","http://www.androiddrawer.com/wp-content/uploads/2013/05/Subway-Surfers-screenshot-4-3.png","http://www.androiddrawer.com/wp-content/uploads/2013/05/Subway-Surfers-screenshot-4-4.png" 

It seems that this can be converted into String[].

use like

String[] result = above_string_variable.split(",");

In your case

use

String[] result = c.getString(TAG_NAME).split(",");

instead of

String name = c.getString(TAG_NAME);
Sign up to request clarification or add additional context in comments.

5 Comments

This will give him an array of strings but won't remove quotes around values.
@Gustek and where is quotes? I didn't understand what u are saying?
"value","value","value" if You split it by comas You are left with array {"\"value\"", "\"value\"", "\"value\"" }
Thanks dear. The answer was brilliant and efficient. But I should have removed quotation marks too.
Hey @NamikazeMinato feel free to update the answer. I will be happy.. :)
0
products = json.getJSONArray(TAG_PRODUCTS);
String[] result = new String[products.length()];
for(int i=0;i<products.length();i++) {

JSONObject c = products.getJSONObject(i);
    result[i] = c.getString(TAG_NAME);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.