Date post: | 15-Jan-2015 |
Category: |
Documents |
Upload: | shyamala-prayaga |
View: | 2,225 times |
Download: | 4 times |
JSON Basics
Shyamala Prayaga
Yodlee®, Inc. Copyright © 2009 - 04/10/2023 - CONFIDENTIAL 2
Agenda
• What is JSON?
• Why JSON?
• Pros and Cons
• JSON Syntax
• JSON Parser
• Accessing Data in JSON
Yodlee®, Inc. Copyright © 2009 - 04/10/2023 - CONFIDENTIAL 3
What is JSON?
• Stands for JavaScript Object Notation
• Is lightweight format for exchanging data between the client and server.
• Easy for humans to read and write.
• Easy for machines to parse and generate
• Often used in Ajax applications due to
– Its simplicity– its format which is based on JavaScript object literals
Yodlee®, Inc. Copyright © 2009 - 04/10/2023 - CONFIDENTIAL 4
Why JSON?
• JSON is recognized natively by JavaScript
• Simple format
• The easiness of reading
• The easiness of using
• Lighter than XML
Yodlee®, Inc. Copyright © 2009 - 04/10/2023 - CONFIDENTIAL 5
Benefits over XML
JSON XMLJSON object are type XML data is typeless
JSON types: string, number, array, boolean XML data are all string
Data is readily accessible as JSON objects XML data needs to be parsed
Retrieving value is easy Retrieving value is difficult
JSON is supported by all browsers Cross browser XML parsing can be tricky
Simple API Complex API
Supported by many Ajax toolkit Not fully supported by Ajax toolkit
Fast object de-serialization in JavaScript Slower de-serialization in JavaScript
Fully automated way of de-serializing/serializing JavaScript objects
Developers have to write JavaScript code to serialize/de-serialize to/from XML
Yodlee®, Inc. Copyright © 2009 - 04/10/2023 - CONFIDENTIAL 6
Pros and Cons
Pros Cons
Fast to parse No namespace support, hence poor extensibility
Good support for all browsers Limited development tools support
Supported by many languages No support for formal grammar definition
Concise format: name/value pair -based approach Easy to read
Easy to write
Yodlee®, Inc. Copyright © 2009 - 04/10/2023 - CONFIDENTIAL 7
JSON Syntax
• For objects start the object with “{“ and end it with “}”
• For members (properties), use pairs of string : value and separate them by commas
• For arrays put the arrays between []
• For elements put the values directly separated by commas
Yodlee®, Inc. Copyright © 2009 - 04/10/2023 - CONFIDENTIAL 8
Example
var myFirstJSON = {
"firstName" : "John",
"lastName" : "Doe",
"age" : 23
};
Yodlee®, Inc. Copyright © 2009 - 04/10/2023 - CONFIDENTIAL 9
JSON Parser
• You can convert JSON object into JSON text
• Object to Text Conversion
var myJSONText = myObject.toJSONString();
Yodlee®, Inc. Copyright © 2009 - 04/10/2023 - CONFIDENTIAL 10
Accessing Data in JSON
• The most common way to access JSON data is through dot notation.
var myObject = { 'color' : 'blue' };
document.writeln(myObject.color);
Yodlee®, Inc. Copyright © 2009 - 04/10/2023 - CONFIDENTIAL 11
Questions?