I am in learning phase and strucked that what to choose JSON generator or JSON parser when response is coming from 3rd party and type REST webservice.
In Apex guide it showing
Using the JSONGenerator class methods, you can generate standard JSON-encoded content.
Use the JSONParser class methods to parse JSON-encoded content.
Can anyone please guide me with example.
Best Answer
The two are opposite features. The JSONGenerator takes data input and outputs JSON strings. The JSONParser takes JSON string input and outputs objects.
If you're sending data to an outside source, either for a client calling your code, or sending data to a server, use the generator. If you're receiving data from an outside source, either from a client or from a server response, use the parser.
However, if you want efficient code, you should use neither, and stick with JSON.serialize, JSON.deserialize, or JSON.deserializeUntyped. JSON.serialize is a quicker version of the JSONParser, and the deserialize methods are a quicker version of the JSONGenerator.
The JSONGenerator has an example, which I'll copy-paste for you here:
The JSONParser also has an example, which I've also included here.
As you can see, reading and writing are both very tedious to do, and you should consider using these classes only as a last resort.