Android Parse JSON Array

后端 未结 3 1794
别跟我提以往
别跟我提以往 2020-12-11 10:20

How Can I parse a JSON ARRAY to get the data without the [] and \"\"

here is the json

\"formattedAddress\": [
\"23, Damansara - Puchong Hwy (Bandar          


        
相关标签:
3条回答
  • 2020-12-11 10:57

    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);
      }
    
    0 讨论(0)
  • 2020-12-11 11:01

    Use JSONArray.join():

    getJSONArray("formattedAddress").join(","));
    
    0 讨论(0)
  • 2020-12-11 11:13

    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("\"", ""));
    
    0 讨论(0)
提交回复
热议问题