从Json获取文本并将其添加到String []

时间:2013-05-23 06:48:32

标签: android arrays json android-arrayadapter

我想从Json获取一个文本(一些网址)并将它们直接添加到android中的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"

我想将此文本放入String []。我知道我可以使用这种方法:

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

但它给了我String而不是String []。

2 个答案:

答案 0 :(得分:3)

查看你的字符串

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

似乎可以将其转换为String []。

使用

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

在你的情况下

使用

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

而不是

String name = c.getString(TAG_NAME);

答案 1 :(得分: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);
}