json support in salesforce - winter 12
DESCRIPTION
What is JSON. What is use of JSON. Working with JSON in SalesforceTRANSCRIPT
By – Jitendra Zaa
Salesforce JSON supportWinter 12
1
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
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
XML Example
<Person><FirstName>Foo</FirstName><LastName>Bar</LastName></person>
4
JSON Example
{“FirstName” : “Foo”,“LastName” : “Bar”
}
5
JSON Advantage over XML
• XML is Verbose.• More size than of equivalent JSON representation
because of repeated Tags.• JSON Simple to represent
6
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
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
Nested Object JSON
{“FirstName” : “Foo”,“LastName” : “Bar”,“Address” :{
“city” : “Pune”,“state” : “Maharashtra”,“country” : “India”
}}
9
How to Work with JSON – Before Winter 12
• AppExchange and other codes were available for Parsing JSON.
• Performance Overhead.• Learning curve more.
10
Deserialize JSON – Winter 12 API
JSON String :{
“FirstName” : “Foo”,“LastName” : “Bar”
}
Equivalent Apex Class:Class Person{
public String FirstName;public String LastName;
}
11
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
Serialize Apex Class to JSON
JSON.serialize(object);
13
Object to convert to JSON
JSON MIME Type
• application/json
14
Visualforce Page Output as JSON
• contentType="application/x-JavaScript; charset=utf-8"
• showHeader="false" • standardStylesheets="false" • sidebar="false"
15
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
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
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
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