Gastón Saillén
Gastón Saillén

Reputation: 13129

How to add arraylist into Map

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

{
 "email_address": "[email protected]",
    "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","[email protected]");
                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\":\"[email protected]\"," +
                "\"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/.

Upvotes: 1

Views: 41

Answers (1)

Napster
Napster

Reputation: 1373

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);

Upvotes: 1

Related Questions