I have a class called wrapper. I want to serialize a list of this class objects using JSON.serialize. The problem is I don't want to serialize all the properties available in the class. I want to serialize only a few fields which will be selected by the user, The user might select A1,A3,A4 one time, the next time A1,A6. I will have a remote action method which will receive a comma separted list of fields the user selected. This method should return a JSON object with values. Is there a way available to do this?

public class Wrapper {
public String src {get; set;}
public String  A1 {get;set}
public String  A2 {get;set}
public String  A3 {get;set}
public String  A4 {get;set}
public String  A5 {get;set}
public String  A6 {get;set}

1 Answer 1


If you transfer the selected values into a Map where the key values match your Wrapper class field names, the JSON generated will be the same (except including only the selected values);

Wrapper w = ...;
Map<String, String> m = new Map<String, String>();
if (includeA1) m.put('A1', w.A1);
if (includeA2) m.put('A2', w.A2);
if (includeA3) m.put('A3', w.A3);

String jsonString = JSON.serialize(m);

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .