JSON Tutorial - Amirye.ir

2y ago
77 Views
9 Downloads
1.24 MB
36 Pages
Last View : 2m ago
Last Download : 3m ago
Upload by : Xander Jaffe
Transcription

JSON Tutorial ﺗﻬﻴﻪ ﮐﻨﻨﺪﻩ ا http://www.amirye.ir

ABOUT THE TUTORIALJSON TutorialJSON or JavaScript Object Notation is a lightweight is a text-based open standard designed for humanreadable data interchange. The JSON format was originally specified by Douglas Crockford, and isdescribed in RFC 4627. The official Internet media type for JSON is application/json. The JSON filenameextension is .json.This tutorial will help you in understanding JSON and how to use it within various programminglanguages like PHP, PERL, Python, Ruby, Java etc.AudienceThis tutorial has been designed to help beginners understand basic functionality of JavaScript ObjectNotation (JSON) to develop data interchange format. After completing this tutorial you will find yourselfat a moderate level of expertise in using JSON with Javscript, Ajax, Perl etc from where you can takeyourself to next levels.PrerequisitesBefore proceeding with this tutorial you should have a basic understanding of how web application workover HTTP and we assume that you have basic knowledge of JavaScript.Copyright & Disclaimer Notice Allthe content and graphics on this tutorial are the property of tutorialspoint.com. Any content fromtutorialspoint.com or this tutorial may not be redistributed or reproduced in any way, shape, or formwithout the written permission of tutorialspoint.com. Failure to do so is a violation of copyright laws.This tutorial may contain inaccuracies or errors and tutorialspoint provides no guarantee regarding theaccuracy of the site or its contents including this tutorial. If you discover that the tutorialspoint.com siteor this tutorial content contains some errors, please contact us at webmaster@tutorialspoint.comTUTORIALS POINTSimply Easy Learning

Table of ContentJSON Tutorial . 2Audience . 2Prerequisites . 2Copyright & Disclaimer Notice. 2JSON - Overview . 6Uses of JSON . 6Characteristics of JSON . 6Simple Example in JSON . 7JSON - Syntax . 9JSON - DataTypes . 10Number . 10SYNTAX: . 11EXAMPLE: . 11String . 11SYNTAX: . 11EXAMPLE: . 11Boolean . 11SYNTAX: . 11EXAMPLE: . 12Array. 12SYNTAX: . 12EXAMPLE: . 12Object . 12SYNTAX: . 12EXAMPLE: . 12Whitespace . 13SYNTAX: . 13EXAMPLE: . 13null . 13SYNTAX: . 13EXAMPLE: . 13JSON Value . 13SYNTAX: . 14EXAMPLE: . 14JSON – Objects . 15Creating Array Objects . 16JSON - Schema . 18TUTORIALS POINTSimply Easy Learning

JSON Schema Validation Libraries . 18JSON Schema Example. 19JSON - Comparison . 21Verbose . 21Arrays Usage . 21Parsing . 21Example . 21JSON. 21XML . 21JSON with PHP. 22Environment . 22JSON Functions . 22Encoding JSON in PHP (json encode) . 22SYNTAX: . 22PARAMETERS: . 22EXAMPLE . 22Decoding JSON in PHP (json decode) . 23SYNTAX: . 23PARAMATERS: . 23EXAMPLE . 23JSON with PERL. 25Environment . 25JSON Functions . 25Encoding JSON in Perl (encode json) . 25SYNTAX: . 25EXAMPLE . 26Decoding JSON in Perl (decode json) . 26SYNTAX: . 27EXAMPLE . 27JSON with Python . 28Environment . 28JSON Functions . 28Encoding JSON in Python (encode) . 28SYNTAX: . 28EXAMPLE . 28Decoding JSON in Python (decode) . 29SYNTAX: . 29EXAMPLE . 29JSON with Ruby. 30TUTORIALS POINTSimply Easy Learning

Environment . 30Parsing JSON using Ruby. 30JSON with Java . 32Environment . 32Mapping between JSON and Java entities. 32Encoding JSON in Java . 32Decoding JSON in Java . 33JSON with Ajax . 35Example . 35Cricketer Details. 36Cricketer Details. 36TUTORIALS POINTSimply Easy Learning

1CHAPTERJSON - OverviewJSON or JavaScript Object Notation is a lightweight text-based open standard designed for human-readabledata interchange. Conventions used by JSON are known to programmers which include C, C , Java, Python,Perl etc. JSON stands for JavaScript Object Notation. This format was specified by Douglas Crockford. This was designed for human-readable data interchange It has been extended from the JavaScript scripting language. The filename extension is .json JSON Internet Media type is application/json The Uniform Type Identifier is public.jsonUses of JSON It is used when writing JavaScript based application which includes browser extension and websites. JSON format is used for serializing & transmitting structured data over network connection. This is primarily used to transmit data between server and web application. Web Services and API.s use JSON format to provide public data. It can be used with modern programming languages.Characteristics of JSON Easy to read and write JSON.TUTORIALS POINTSimply Easy Learning

Lightweight text based interchange format Language independent.Simple Example in JSONExample shows Books information stored using JSON considering language of books and there editions:{"book": [{"id":"01","language": "Java","edition": "third","author": "Herbert Schildt"},{"id":"07","language": "C ","edition": "second""author": "E.Balagurusamy"}]}After understanding the above program we will try another example, let's save the below code as json.htm: html head title JSON example /title script language "javascript" var object1 { "language" : "Java", "author" : "herbert schildt" };document.write(" h1 JSON with JavaScript example /h1 ");document.write(" br ");document.write(" h3 Language " object1.language " /h3 ");document.write(" h3 Author " object1.author " /h3 ");var object2 { "language" : "C ", "author" : "E-Balagurusamy" };document.write(" br ");document.write(" h3 Language " object2.language " /h3 ");document.write(" h3 Author " object2.author " /h3 ");document.write(" hr / ");document.write(object2.language " programming language can be studied " "from book written by " object2.author);document.write(" hr / "); /script /head body /body /html Now let's try to open json.htm using IE or any other javascript enabled browser, this produces the following result:TUTORIALS POINTSimply Easy Learning

You can refer to JSON Objects chapter about more information on JSON objects.TUTORIALS POINTSimply Easy Learning

2CHAPTERJSON - SyntaxLet’s have a quick look on JSON basic syntax. JSON syntax is basically considered as subset ofJavaScript syntax, it includes the following: Data is represented in name/value pairs Curly braces hold objects and each name is followed by ':'(colon), the name/value pairs are separated by ,(comma). Square brackets hold arrays and values are separated by ,(comma).Below is a simple example:{"book": [{"id":"01","language": "Java","edition": "third","author": "Herbert Schildt"},{"id":"07","language": "C ","edition": "second""author": "E.Balagurusamy"}]}JSON supports following two data structures: Collection of name/value pairs: This Data Structure is supported by different programming language.Ordered list of values: It includes array, list, vector or sequence etc.TUTORIALS POINTSimply Easy Learning

3CHAPTERJSON - DataTypesThere are following datatypes supported by JSON format:TypeDescriptionNumberdouble- precision floating-point format in JavaScriptStringdouble-quoted Unicode with backslash escapingBooleantrue or falseArrayan ordered sequence of valuesValueit can be a string, a number, true or false, null etcObjectan unordered collection of key:value pairsWhitespacecan be used between any pair of tokensNullEmptyNumber It is a double precision floating-point format in JavaScript and it depends on implementation. Octal and hexadecimal formats are not used. No NaN or Infinity is used in Number.The following table shows number types:TypeDescriptionIntegerDigits 1-9, 0 and positive or negativeFractionFractions like .3, .9ExponentExponent like e, e , e-,E, E , E-TUTORIALS POINTSimply Easy Learning

SYNTAX:var json-object-name { string : number value, .}EXAMPLE:Example showing Number Datatype, value should not be quoted:var obj {marks: 97}String It is a sequence of zero or more double quoted Unicode characters with backslash escaping. Character is a single character string i.e. a string with length 1.The table shows string types:TypeDescription"double quotation\reverse solidus/SolidusBBackspaceFform feedNnew lineRcarriage returnThorizontal tabUfour hexadecimal digitsSYNTAX:var json-object-name { string : "string value", .}EXAMPLE:Example showing String Datatype:var obj {name: 'Amit'}BooleanIt includes true or false values.SYNTAX:var json-object-name { string : true/false, .}TUTORIALS POINTSimply Easy Learning

EXAMPLE:var obj {name: 'Amit', marks: 97, distinction: true}Array It is an ordered collection of values. These are enclosed square brackets which means that array begins with .[. and ends with .]. The values are separated by ,(comma). Array indexing can be started at 0 or 1. Arrays should be used when the key names are sequential integers.SYNTAX:[ value, .]EXAMPLE:Example showing array containing multiple objects:{"books": [{ "language":"Java" , "edition":"second" },{ "language":"C " , "lastName":"fifth" },{ "language":"C" , "lastName":"third" }]}Object It is an unordered set of name/value pairs. Object are enclosed in curly braces that is it starts with '{' and ends with '}'. Each name is followed by ':'(colon) and the name/value pairs are separated by , (comma). The keys must be strings and should be different from each other. Objects should be used when the key names are arbitrary stringsSYNTAX:{ string : value, .}EXAMPLE:Example showing Object:TUTORIALS POINTSimply Easy Learning

{"id": "011A","language": "JAVA","price": 500,}WhitespaceIt can be inserted between any pair of tokens. It can be added to make code more readable. Example showsdeclaration with and without whitespace:SYNTAX:{string:"",.}EXAMPLE:var i "var j "sachin";saurav"nullIt means empty type.SYNTAX:nullEXAMPLE:var i null;if(i 1){document.write(" h1 value is 1 /h1 ");}else{document.write(" h1 value is null /h1 ");}JSON ValueIt includes: number (integer or floating point) string boolean arrayTUTORIALS POINTSimply Easy Learning

object nullSYNTAX:String Number Object Array TRUE FALSE NULLEXAMPLE:var i 1;var j "sachin";var k null;TUTORIALS POINTSimply Easy Learning

4CHAPTERJSON – ObjectsCreating Simple ObjectsJSON objects can be created with Javascript. Let us see various ways of creating JSON objects usingJavascript: Creation of an empty Object:var JSONObj {}; Creation of new Object:var JSONObj new Object(); Creation of an object with attribute bookname with value in string, attribute price with numeric value.Attributes is accessed by using '.' Operator:var JSONObj { "bookname ":"VB BLACK BOOK", "price":500 };This is an example which shows creation of an object in javascript using JSON, save the below codeas json object.htm: html head title Creating Object JSON with JavaScript /title script language "javascript" var JSONObj { "name" : "tutorialspoint.com", "year" : 2005 };document.write(" h1 JSON with JavaScript example /h1 ");document.write(" br ");document.write(" h3 Website Name " JSONObj.name " /h3 ");document.write(" h3 Year " JSONObj.year " /h3 "); /script /head body /body /html TUTORIALS POINTSimply Easy Learning

Now let's try to open json object.htm using IE or any other javascript enabled browser, this produces the followingresult:Creating Array ObjectsBelow example shows creation of an array object in javascript using JSON, save the below codeasjson array object.htm: html head title Creation of array object in javascript using JSON /title script language "javascript" document.writeln(" h2 JSON array object /h2 ");var books { "Pascal" : [{ "Name" : "Pascal Made Simple", "price" : 700 },{ "Name" : "Guide to Pascal", "price" : 400 }],"Scala" : [{ "Name" : "Scala for the Impatient", "price" : 1000 },{ "Name" : "Scala in Depth", "price" : 1300 }]}var i 0document.writeln(" table border '2' tr ");for(i 0;i books.Pascal.length;i ){document.writeln(" td ");document.writeln(" table border '1' width 100 ");document.writeln(" tr td b Name /b /td td width 50 " books.Pascal[i].Name " /td /tr ");document.writeln(" tr td b Price /b /td td width 50 " books.Pascal[i].price " /td /tr ");document.writeln(" /table ");document.writeln(" /td ");}for(i 0;i books.Scala.length;i ){document.writeln(" td ");document.writeln(" table border '1' width 100 ");document.writeln(" tr td b Name /b /td td width 50 " books.Scala[i].Name " /td /tr ");document.writeln(" tr td b Price /b /td td width 50 "TUTORIALS POINTSimply Easy Learning

books.Scala[i].price " /td /tr ");document.writeln(" /table ");document.writeln(" /td ");}document.writeln(" /tr /table "); /script /head body /body /html Now let's try to open json array object.htm using IE or any other javascript enabled browser,

tutorialspoint.com or this tutorial may not be redistributed or reproduced in any way, shape, or form without the written permission of tutorialspoint.com. Failure to do so is a violation of copyright laws. This tutorial may contain inaccuracies or errors and tu

Related Documents:

Table of Contents . Disclaimer . 1 Introduction . 3 . Brief Introduction to JSON and Querying JSON in Oracle 12c 4 Storing JSON 4 JSON Path Expressions 5 Querying JSON 5 Indexing JSON 6 NoBench Benchmark 8 Benchmark Details 8 Performance Enhancements for JSON data 10 JSON with In-Memory Columnar Store (

JSON-to-JSON transformation languages enable the transformation of a JSON document into another JSON document. As JSON is grad-ually becoming the most used interchange format on the Internet there is a need for transformation languages that can transform the data stored in JSON in order for the data to be used with other sys-tems.

JSON and SQL in Oracle Database Oracle Database 12c Release 1 added many great features for native support of JSON in tables and via SQL. "IS JSON" constraint for existing types -there is no JSON type -(N)VARCHAR2, (N)CLOB, BLOB, RAW JSON operators in SQL -JSON_VALUE, JSON_QUERY, JSON_TABLE, JSON_EXISTS, IS JSON JSON Dataguide

The JSON Web Algorithms (JWA) specification enumerates cryptographic algorithms and identifiers to be used with the JSON Web Signature (JWS) , JSON Web Encryption (JWE), and JSON Web Key (JWK) specifications. All these specifications utilize JavaScript Object Notation (JSON) based da

Java API for Processing JSON (JSON-P) Streaming API to produce/consume JSON - Similar to StAX API in XML world Object model API to represent JSON - Similar to DOM API in XML world Align with Java EE 7 schedules JSR Supporters - fasterxml.com(Jackson), Doug Crockford(json.org) JSR-353

SQL/JSON data model A sequence of SQL/JSON items, each item can be (recursively) any of: SQL/JSON scalar — non-null value of SQL types: Unicode character string, numeric, Boolean or datetime SQL/JSON null, value that is distinct from any value of any SQL type (not the same as NULL)

SQL/JSON in SQL-2016 SQL/JSON data model A sequence of SQL/JSON items, each item can be (recursively) any of: SQL/JSON scalar — non-null value of SQL types: Unicode character string, numeric, Boolean or datetime SQL/JSON null, value that is distinct from any value of any SQL type (not the same as NULL)

Java XML & JSON Programming JavaXML-SAX,DOM, XSLT,XPath,JAXB2,org.json. 2. Java XML Programming -see XML Samples. 2. Java JSON Programming - see JSON samples. Section Conclusions Java XML-JSON Programming for easy sharing Pleasereview JavaXMLandJSONsamples. Communicate & Exchange Ideas