androiddictionary

How to add arraylist into Map


I'm trying to mimic this structure to post data to Mailchimp, I'm using volley

{
 "email_address": "testchimp@gmail.com",
    "status": "subscribed",
    "merge_fields": {
        "FNAME": "testmailchimp",
        "LNAME": "testingit",
     }  
}

I have done this, but I need to add the FNAME and LNAME and I don't know how to do it right

 @Override
            protected Map<String,String> getParams(){
                Map<String,String> params = new HashMap<String, String>();
                params.put("email_address","testchimp@gmail.com");
                params.put("status","unsubscribed");
                //here i want to add the arraylist FNAME and LNAME
                return params;
            }

Update

I solved my problem with this:

final JSONObject jsonBody = new JSONObject("{\"email_address\":\"mailchimptester@gmail.com\"," +
                "\"status\":\"unsubscribed\"," +
                "\"merge_fields\":{\"FNAME\":\"test\",\"LNAME\":\"teste\"}}");

So with this I can do the JSON how I want without maps, and then validate it here https://jsonlint.com/.


Solution

  • It's not an ArrayList it would be a Map. You can do it using this code

    HashMap<String,String> merge_fields = new HashMap<>();
    merge_fields.put("FNAME","testmailchimp");
    merge_fields.put("LNAME","testingit");
    String merge_fields_param = new JSONObject(merge_fields).toString();
    params.put("merge_fields",merge_fields_param);