Back to Winter '12 Force.com Platform Release
Winter '12 Force.com Platform Release
JSON Support in Apex
Native JavaScript Object Notation (JSON) support has been added to Apex in Winter ‘12. Using JSON classes, you can now parse JSON content and serialize Apex objects into the JSON format.
The following classes have been added for JSON support:
System.JSON: Contains methods for serializing Apex objects into JSON format and deserializing JSON content that was serialized using the serialize method in this class.System.JSONGenerator: Contains methods used to serialize Apex objects into JSON content using the standard JSON encoding.System.JSONParser: Represents a parser for JSON-encoded content.
Here are two examples of this in action:
Listinvoices = new List (); invoices.add(inv1); invoices.add(inv2); // Serialize the list of InvoiceStatement objects. String JSONString = JSON.serialize(invoices); System.debug('Serialized list of invoices into JSON format: ' + JSONString); // Deserialize the list of invoices from the JSON string. List deserializedInvoices = (List )JSON.deserialize(JSONString, List .class); System.assertEquals(invoices.size(), deserializedInvoices.size());
Http httpProtocol = new Http(); // Create HTTP request to send. HttpRequest request = new HttpRequest(); …. HttpResponse response = httpProtocol.send(request); // Create JSON parser with the HTTP response body // as an input string. JSONParser parser = JSON.createParser(response.getBody());
