How Can I parse a JSON ARRAY to get the data without the [] and ""
here is the json
"formattedAddress": [
"23, Damansara - Puchong Hwy (Bandar Puchong Jaya)",
"47100 Puchong Batu Dua Belas, Selangor",
"Malaysia"
]
my code:
poi.setFormattedAddress(jsonArray.getJSONObject(i).getJSONObject("location").getJSONArray("formattedAddress").toString());
output: [ "23, Damansara - Puchong Hwy (Bandar Puchong Jaya)", "47100 Puchong Batu Dua Belas, Selangor", "Malaysia" ]
I just want the data. as:
23, Damansara - Puchong Hwy (Bandar Puchong Jaya), 47100 Puchong Batu Dua Belas, Selangor, Malaysia
Thanks
-
1may be your JSon array is not correct. please check this link tutorialspoint.com/android/android_json_parser.htmPavya– Pavya2015年01月21日 03:58:13 +00:00Commented Jan 21, 2015 at 3:58
-
Yeah I think your json is not correct....eLemEnt– eLemEnt2015年01月21日 04:03:34 +00:00Commented Jan 21, 2015 at 4:03
-
try reading the data as an array of Strings. String[]toidiu– toidiu2015年01月21日 04:06:38 +00:00Commented Jan 21, 2015 at 4:06
3 Answers 3
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
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("\"", ""));
3 Comments
Try something like this:
public String getFormattedAddressFromArray(JSONArray array) {
List<String> strings = new ArrayList<String>();
try {
for (int i = 0; i < array.length(); i++) {
strings.add(array.getString(i));
}
} catch (JSONException e) {
e.printStackTrace();
}
return TextUtils.join(", ", strings);
}
Comments
Use JSONArray.join():
getJSONArray("formattedAddress").join(","));