+ Start a Discussion
Abhishek KedariAbhishek Kedari 

Modify JSON object in salesforce

   I am creating JSON object in salesforce using 
JSONGenerator gen = JSON.createGenerator(true);

I keep adding some fields and values at different hierarchy in this object, but while doing this if I get some different values later for my other computations, I would like to modify already created values in this above object.

So my question is whether I can modify the JSON object or not ? 
If yes, How? It would be good if you provide some good links or sample code.


Gaurav NirwalGaurav Nirwal
public class surveyAnswer
 Id contact;
 Integer surveyId;
 String questionText;
 String questionId;
 String questionAnswer;
 public surveyAnswer(Id c, Integer s, String qt, String qi, String qa)
  contact = c;
  surveyId = s;
  questionText = qt;
  questionId = qi;
   questionAnswer = qa;           
 list<surveyAnswer> answers = new list <surveyAnswer>();
 contact con = [select id from contact limit 1];
 surveyAnswer thisAns1 = new surveyAnswer(con.id, 12345, 'blah1','id1','yup');
 surveyAnswer thisAns2 = new surveyAnswer(con.id, 12345, 'blah1','id2','nope');
 //add the objects
 //seraialize them
 String JSONString = JSON.serialize(answers);
 //This dies
 list<surveyAnswer> answersObjs = (List<surveyAnswer>)JSON.deserialize(JSONString,List<surveyAnswer>.class);