0

It seems like in some instances apex is serializing Lists differently. I need it to serialize a list to a json array, however I'm getting a comma separated list of objects.

Why does this give me a csv string of objects that looks like this ?({property=name, value=mike}, {property=age, value=47})

Map<String, Object> m = new Map<String, Object>();
m.put('property', 'name');
m.put('value', 'mike');

Map<String, Object> m2 = new Map<String, Object>();
m2.put('property', 'age');
m2.put('value', 47);

List<Object> x = new List<Map<String, Object>>();
x.add(m);
x.add(m2);

string y = JSON.serialize(x);

System.debug(x);

Whereas this gives me an array of strings that looks like this? ["January","February","March","April","May","June","July","August","September","October","November","December"]

List<String> months = new List<String>{'January','February','March','April','May','June','July','August','September','October','November','December'};
string a = JSON.serialize(months);
System.debug(a);

1 Answer 1

3

You are serializing variable x into variable y ,and still priting variable x which is unchanged.

fix: Print variable y

Map<String, Object> m = new Map<String, Object>();
m.put('property', 'name');
m.put('value', 'mike');

Map<String, Object> m2 = new Map<String, Object>();
m2.put('property', 'age');
m2.put('value', 47);

List<Object> x = new List<Map<String, Object>>();
x.add(m);
x.add(m2);

string y = JSON.serialize(x);

System.debug(y);
1
  • Thank you sir, perhaps it is time for me to take a break. Commented Nov 26, 2019 at 21:29

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.