1

I have a code JSONGenerator gen = JSON.createGenerator(true); I am not exactly getting an understanding of this line.

The next lines use

 gen.writeStartObject();
 gen.writeStringField('tName', TMsg);
2

I find that JSONGenerator is rarely needed. To generate or parse JSON, using the serialize or deserialize methods of the JSON class directly is usually the best way to go to generate valid JSON or parse JSON.

If you want data holder classes generated, also consider using https://json2apex.herokuapp.com/.

Methods like your writeStringField will ensure that a double quote embedded in the string will be correctly escaped so it is not mistaken for the end of the JSON string. The serialize methods do that too.

| improve this answer | |
  • Thanks a lot Keith . JsonGenerator was used by us to create a response message in json format – user75937 Nov 15 '19 at 4:44
0

Using the JSONGenerator class methods, you can generate standard JSON-encoded content.

You can construct JSON content, element by element, using the standard JSON encoding. To do so, use the methods in the JSONGenerator class.

writeStartObject()

    Writes the starting marker of a JSON object ('{').

writeStringField(fieldName, stringValue)

    Writes a field name and value pair using the specified field name and string value 
| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.