JSON Introduction

Introduction


As we know Ajax is a web development technology that makes the server responses faster by enabling the client-side scripts to retrieve only the required data from the server without retrieving a complete web page on each request, which will minimize the data transferred from the server.

These requests usually retrieve xml formatted response, the xml responses are then parsed in the JavaScript code to render the results. Which complicate the JavaScript code

The idea of JSON (JavaScript Object Notation) is to make the response a specific data structure that can be easily parsed by the JavaScript code.


Advantages


1- lightweight data-interchange format

2- Easy for humans to read and write

3- Easy for machines to parse and generate

4- JSON can be parsed trivially using the eval() procedure in JavaScript

5- JSON Supports: ActionScript, C, C#, ColdFusion, E, Java, JavaScript, ML, Objective CAML, Perl, PHP, Python, Rebol, Ruby, and Lua.


.
Syntax


The JSON Syntax is the convention which you will use it to generate data, it’s near to the C family language, so it can be parsed easily in the C family languages.


For objects start the object with “{“ and end it with “}”

object

{}
{ members }

· For members (properties), use pairs of string : value and separate them by commas

members

string : value
members , string : value

· For arrays put the arrays between []

array

[]
[ elements ]

· For elements put the values directly separated by commas

elements

value
elements , value

· Values can be string, number, object, array, true, false, null


EXAMPLES

JSON


{"menu": {


"id": "file",


"value": "File:",


"popup": {


"menuitem": [


{"value": "New", "onclick": "CreateNewDoc()"},


{"value": "Open", "onclick": "OpenDoc()"},


{"value": "Close", "onclick": "CloseDoc()"} ]


}


}}


XML
<menu id="file" value="File" >

<popup>

<menuitem value="New" οnclick="CreateNewDoc()" />

<menuitem value="Open" οnclick="OpenDoc()" />

<menuitem value="Close" οnclick="CloseDoc()" />

</popup>

</menu>


Server side code
The following code will be generated in the server side to retrieve the server time, and in one step in the client side it will be rendered to JavaScript

Java
<%@ page language="java" import="java.util.*" %>
<%Date date = new Date(); %>alert("The server time is: <%Úte%>");
<SPAN style="mso-tab-count: 1"> ASP.NET


<%@ page language="C#" %> alert("The server time is: <%=System.Date.Now.ToString()%>");

PHP
alert("The server time is: <?=time()?>");

Client Side
JavaScript
//XMLHttpRequest completion function
var myOnComplete = function(responseText, responseXML){eval(responseText);}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值