json support in salesforce - winter 12
DESCRIPTION
What is JSON. What is use of JSON. Working with JSON in SalesforceTRANSCRIPT
![Page 1: JSON Support in Salesforce - winter 12](https://reader035.vdocument.in/reader035/viewer/2022062513/555eddd4d8b42ab6408b4d24/html5/thumbnails/1.jpg)
By – Jitendra Zaa
Salesforce JSON supportWinter 12
1
![Page 2: JSON Support in Salesforce - winter 12](https://reader035.vdocument.in/reader035/viewer/2022062513/555eddd4d8b42ab6408b4d24/html5/thumbnails/2.jpg)
Agenda
• What is JSON• Advantage of JSON over XML• Other reasons to use JSON• JSON Syntax / Guidelines• Deserialize and Serialize in Single line - Winter 12• Visualforce Page Output as JSON • Google Map API - Demo
2
![Page 3: JSON Support in Salesforce - winter 12](https://reader035.vdocument.in/reader035/viewer/2022062513/555eddd4d8b42ab6408b4d24/html5/thumbnails/3.jpg)
What is JSON
• JSON Stands for “JavaScript Object Notation”.• Language Independent representation of objects.• Parsers widely available in many languages.• Alternative to XML.• JSON can be transmitted over http / https.
3
![Page 4: JSON Support in Salesforce - winter 12](https://reader035.vdocument.in/reader035/viewer/2022062513/555eddd4d8b42ab6408b4d24/html5/thumbnails/4.jpg)
XML Example
<Person><FirstName>Foo</FirstName><LastName>Bar</LastName></person>
4
![Page 5: JSON Support in Salesforce - winter 12](https://reader035.vdocument.in/reader035/viewer/2022062513/555eddd4d8b42ab6408b4d24/html5/thumbnails/5.jpg)
JSON Example
{“FirstName” : “Foo”,“LastName” : “Bar”
}
5
![Page 6: JSON Support in Salesforce - winter 12](https://reader035.vdocument.in/reader035/viewer/2022062513/555eddd4d8b42ab6408b4d24/html5/thumbnails/6.jpg)
JSON Advantage over XML
• XML is Verbose.• More size than of equivalent JSON representation
because of repeated Tags.• JSON Simple to represent
6
![Page 7: JSON Support in Salesforce - winter 12](https://reader035.vdocument.in/reader035/viewer/2022062513/555eddd4d8b42ab6408b4d24/html5/thumbnails/7.jpg)
Why JSON
• REST is very common and supports JSON widely.• REST and JSON are the music of the internet.• Google , Yahoo, D&B web services exposed as REST
with response type JSON.
7
![Page 8: JSON Support in Salesforce - winter 12](https://reader035.vdocument.in/reader035/viewer/2022062513/555eddd4d8b42ab6408b4d24/html5/thumbnails/8.jpg)
JSON Guidelines
Key – Value enclosed in double quotes (String).Value - can again anything of datatype.{} – represents Object[] – represents Array, - Separates data element within Object
Supports basically four data type:1. Boolean2. Number3. String 4. Object
8
![Page 9: JSON Support in Salesforce - winter 12](https://reader035.vdocument.in/reader035/viewer/2022062513/555eddd4d8b42ab6408b4d24/html5/thumbnails/9.jpg)
Nested Object JSON
{“FirstName” : “Foo”,“LastName” : “Bar”,“Address” :{
“city” : “Pune”,“state” : “Maharashtra”,“country” : “India”
}}
9
![Page 10: JSON Support in Salesforce - winter 12](https://reader035.vdocument.in/reader035/viewer/2022062513/555eddd4d8b42ab6408b4d24/html5/thumbnails/10.jpg)
How to Work with JSON – Before Winter 12
• AppExchange and other codes were available for Parsing JSON.
• Performance Overhead.• Learning curve more.
10
![Page 11: JSON Support in Salesforce - winter 12](https://reader035.vdocument.in/reader035/viewer/2022062513/555eddd4d8b42ab6408b4d24/html5/thumbnails/11.jpg)
Deserialize JSON – Winter 12 API
JSON String :{
“FirstName” : “Foo”,“LastName” : “Bar”
}
Equivalent Apex Class:Class Person{
public String FirstName;public String LastName;
}
11
![Page 12: JSON Support in Salesforce - winter 12](https://reader035.vdocument.in/reader035/viewer/2022062513/555eddd4d8b42ab6408b4d24/html5/thumbnails/12.jpg)
Deserialize JSON – Single Line
Person d = (Person )System.JSON.deserialize(jsonString, Person.class);
12
JSON Format String
Determine the output object type of the JSON
![Page 13: JSON Support in Salesforce - winter 12](https://reader035.vdocument.in/reader035/viewer/2022062513/555eddd4d8b42ab6408b4d24/html5/thumbnails/13.jpg)
Serialize Apex Class to JSON
JSON.serialize(object);
13
Object to convert to JSON
![Page 14: JSON Support in Salesforce - winter 12](https://reader035.vdocument.in/reader035/viewer/2022062513/555eddd4d8b42ab6408b4d24/html5/thumbnails/14.jpg)
JSON MIME Type
• application/json
14
![Page 15: JSON Support in Salesforce - winter 12](https://reader035.vdocument.in/reader035/viewer/2022062513/555eddd4d8b42ab6408b4d24/html5/thumbnails/15.jpg)
Visualforce Page Output as JSON
• contentType="application/x-JavaScript; charset=utf-8"
• showHeader="false" • standardStylesheets="false" • sidebar="false"
15
![Page 16: JSON Support in Salesforce - winter 12](https://reader035.vdocument.in/reader035/viewer/2022062513/555eddd4d8b42ab6408b4d24/html5/thumbnails/16.jpg)
Demo
Get distance between two points using Google API
http://maps.googleapis.com/maps/api/distancematrix/json?origins=Nagpur+Maharashtra+india&destinations=Pune+Maharashtra+india&sensor=false
16
![Page 17: JSON Support in Salesforce - winter 12](https://reader035.vdocument.in/reader035/viewer/2022062513/555eddd4d8b42ab6408b4d24/html5/thumbnails/17.jpg)
Response from Google
{ destination_addresses:[ "Pune, Maharashtra, India" ], origin_addresses:[ "Nagpur, Maharashtra, India" ], rows:[ { elements:[ { distance:{ text:"730 km", value:730315 }, duration:{ text:"11 hours 25 mins", value:41126 }, status:"OK" } ] } ], status:"OK"}
17
![Page 18: JSON Support in Salesforce - winter 12](https://reader035.vdocument.in/reader035/viewer/2022062513/555eddd4d8b42ab6408b4d24/html5/thumbnails/18.jpg)
Apex Class Equivalent to JSON responseClass GoogleResponse
{public String[] destination_addresses; public String[] origin_addresses;public Row[] rows;public String status;
}Class Row{
public Elements[] elements;
}Class Elements{
public Values distance;public Values duration;public String status;
}Class Values{
public String text;public String value;
}18
![Page 19: JSON Support in Salesforce - winter 12](https://reader035.vdocument.in/reader035/viewer/2022062513/555eddd4d8b42ab6408b4d24/html5/thumbnails/19.jpg)
Deserialize JSON
String json = '{ "destination_addresses":[ "Pune, Maharashtra, India" ], "origin_addresses":[ "Nagpur, Maharashtra, India" ],"rows":[ { "elements":[ { "distance":{ "text":"730 km", "value":730315 }, "duration":{ "text":"11 hours 25 mins", "value":41126 }, "status":"OK" } ] } ], "status":"OK"}';
GoogleResponse d = (GoogleResponse)System.JSON.deserialize(json, GoogleResponse.class);
19