未加星标

Introduction to JSON + JavaScript

字体大小 | |
[前端(javascript) 所属分类 前端(javascript) | 发布者 店小二04 | 时间 2016 | 作者 红领巾 ] 0人收藏点击收藏

It is a javascript subset and is typically intended for the exchange of information. It does not require the exclusive use of JavaScript, being interpreted by several programming languages.

It is very used to return data coming from a server using AJAX requests to update data in real time. JSON helps to create complex variables in a simple way and has a reduced size, as it is not a markup language (uses brackets, semicolons, etc, instead of opening and closing tags). These characteristics make JSON more responsive than XML .

Some programmers believe that JSON came to substitute XML , but I don’t think so. JSON and XML work in a similar way but are not the same thing. Let’s analyze the similarities and differences and similarities between JSON and XML :

Similarities The both models represent information in text format; Both are auto-descriptive; Both are able of representing complex information, hard to represent in the tabular format. Some examples: compound objects (objects inside objects), hierarchical relations, multivalued attributes, arrays, missing data, etc; Both can be used to transport information in AJAX applications; Both can be considered standard choices for data representation. XML is a W3C standard, while JSON was formalized in RFC 4627; Data represented in XML and JSON can be accessed by any programming language through specific API’s; Differences The information represented in JSON can not be validated by a schema. In XML , you can validate using a DTD and XML Schema; JSON represents the information more compactly. JSON does not allow the execution of processing instructions, however, that is possible in XML . JSON is designed to exchange information, while XML has more applications;

JSONSyntax

Unlike most languages, where we need to define vector size or which data type this vector will be, in JSON + JavaScript it is not necessary to write this information.

Variables declaration and assignment "year" : 2016

A pair with name and value must be represented by name in double quotes, colons, and value. Values can only have 3 basic types: numeric (integer or real), boolean and string. To represent a numeric or boolean value, the use of quotes is not necessary, but if the value is a string (data in text form), the double quotes are required.

To represented a nullvalue: "website" : null JSONobject variable = {"key" : value}
value access >> variable.key Example: <script> var company = {"name" : "Mycompany", "phone" : "999-9999", "city" : "Paris"}; document.write("The name of the company:" + company.name); //print >> The name of the company: Mycompany </script> JSONvector variable = [value1, value2, value3]
value access >> variable[2] (to give access to value 3) Example: <script> var i = 0; var array= ["Name", 42, 6.5, i]; document.write("The position 1 of the array:" + array[1]); //print >> The position 1 of the array: 42 </script> JSON object andvector <script> var array= [ {"name" : "Mycompany", "phone" : "999-9999", "city" : "Paris"}, {"name" : "Mycompany2", "phone" : "888-8888", "city" : "Barcelona"} ]; document.write("The position zero of the array name:" + array[0].name); //show >> The position zero of the array name: Mycompany document.write("The position oneof the array city:" + array[1].city); //show >> The position one of the array city: Barcelona </script> JSON object ofvectors <script> var array= [ {"name" : [0,1,2], "phone" : "999-9999", "city" : "Paris"}, {"name" : "Anyname", "phone" : "888-8888", "city" : ["Barcelona", "Madri"]} ]; document.write("The position array[0].name[1] of the array:" + array[0].name[1]); //shows >> The position array[0].name[1] of the array: 1 document.write("The array position[1].city[0] of the array:" + array[1].city[0]); //shows >> The array position[1].city[0] of the array: Barcelona </script>

JSONis currently one of the best methods to retrieve server information, especially in an application that requires real-time data updates.

We will explore more about JSON and how to build applications in the next posts.

Until next time!

本文前端(javascript)相关术语:javascript是什么意思 javascript下载 javascript权威指南 javascript基础教程 javascript 正则表达式 javascript设计模式 javascript高级程序设计 精通javascript javascript教程

主题: XMLJavaScriptJava
分页:12
转载请注明
本文标题:Introduction to JSON + JavaScript
本站链接:http://www.codesec.net/view/479764.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 前端(javascript) | 评论(0) | 阅读(50)