function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Abhishek KedariAbhishek Kedari 

Modify JSON object in salesforce

Hi,
 
   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.

Thanks,
Abhishek

 
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
 answers.add(thisAns1);
 answers.add(thisAns2);
 //seraialize them
 String JSONString = JSON.serialize(answers);
 system.debug(JSONString);
 //This dies
 list<surveyAnswer> answersObjs = (List<surveyAnswer>)JSON.deserialize(JSONString,List<surveyAnswer>.class);
 system.debug(answersObjs);