Top Banner
By – Jitendra Zaa Salesforce JSON support Winter 12 1
19

JSON Support in Salesforce - winter 12

May 22, 2015

Download

Technology

What is JSON. What is use of JSON. Working with JSON in Salesforce
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: JSON Support in Salesforce - winter 12

By – Jitendra Zaa

Salesforce JSON supportWinter 12

1

Page 2: JSON Support in Salesforce - winter 12

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

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

XML Example

<Person><FirstName>Foo</FirstName><LastName>Bar</LastName></person>

4

Page 5: JSON Support in Salesforce - winter 12

JSON Example

{“FirstName” : “Foo”,“LastName” : “Bar”

}

5

Page 6: JSON Support in Salesforce - winter 12

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

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

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

Nested Object JSON

{“FirstName” : “Foo”,“LastName” : “Bar”,“Address” :{

“city” : “Pune”,“state” : “Maharashtra”,“country” : “India”

}}

9

Page 10: JSON Support in Salesforce - winter 12

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

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

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

Serialize Apex Class to JSON

JSON.serialize(object);

13

Object to convert to JSON

Page 14: JSON Support in Salesforce - winter 12

JSON MIME Type

• application/json

14

Page 15: JSON Support in Salesforce - winter 12

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

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

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

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

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