Android Parse JSON数组 [英] Android Parse JSON Array
问题描述
如何解析JSON ARRAY以获取没有[]和的数据
How Can I parse a JSON ARRAY to get the data without the [] and ""
这里是json
"formattedAddress": [
"23, Damansara - Puchong Hwy (Bandar Puchong Jaya)",
"47100 Puchong Batu Dua Belas, Selangor",
"Malaysia"
]
我的代码:
poi.setFormattedAddress(jsonArray.getJSONObject(i).getJSONObject("location").getJSONArray("formattedAddress").toString());
输出:
[23,Damansara - Puchong Hwy(Bandar Puchong Jaya), 47100 Puchong Batu Dua Belas,雪兰莪,马来西亚]
output: [ "23, Damansara - Puchong Hwy (Bandar Puchong Jaya)", "47100 Puchong Batu Dua Belas, Selangor", "Malaysia" ]
我只想要数据。 as:
I just want the data. as:
23,Damansara - Puchong Hwy(Bandar Puchong Jaya),47100 Puchong Batu Dua Belas,Selangor,Malaysia
23, Damansara - Puchong Hwy (Bandar Puchong Jaya), 47100 Puchong Batu Dua Belas, Selangor, Malaysia
谢谢
推荐答案
使用 JSONArray #join()
加入要素。查看 JSONArray 文档更多信息。
Use JSONArray#join()
to join the elements. Check out the JSONArray docs for more info.
poi.setFormattedAddress(
jsonArray.getJSONObject(i).getJSONObject("location")
.getJSONArray("formattedAddress").join(", ")); // passing ", " as the separator
目前还不清楚引用是否是输入JSON字符串的一部分。如果它们出现在您的联接中,您可以使用 String#replace()
方法轻松删除它们。
It's unclear if the quotations are part of your input JSON string. If they show up in your join, you can easily remove them with the String#replace()
method.
System.out.println(jsonArray.join(", ").replace("\"", ""));
这篇关于Android Parse JSON数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!