About the JSON in JS

Source: Internet
Author: User

Yesterday, according to the boss's request, make a function, there is a need to package parameters from the JSP page, as follows

Before I wrote, each value, I did not give them double quotation marks (single quotation marks), and then the interface has not come out, and also do not error, in this piece spent a long time, the boss can not see, came to me this look at the code, suddenly pointed out this said "you this value does not add double quotation marks Ah, This way JS will treat them as a variable, not a string object, those numbers can not be added, but some strings need double quotation marks to line!!! "With double quotes, the interface comes out right away .... Sure enough is the boss, in this, I also notice the addition of double quotes in JS with the importance of JSON! Here is a blog I find, the parse (), stringify () have been explained, to share to everyone
Http://www.cnblogs.com/gaojun/p/3394274.html
In the early days, XML was generally used as the transmission of structured data over the Internet, but because of its cumbersome parsing and lengthy characters, it was replaced by lightweight JSON. JSON is a strict subset of JavaScript that uses some of the patterns in JavaScript to represent structured data. You can directly pass the JSON-formatted string to eval () for processing, because JSON is a subset of JS, and after Eval becomes a JSON object, you can use '. ' operator directly manipulate attributes;

It is worth noting that JSON is a data format, not a language, although it is similar to the form of object literals defined in JS, but JSON does not belong to JavaScript, because other languages (such as: php,jsp) also have parsing and serialization methods for JSON.
Grammar

 JSON可以表示以下三种值:简单值:如字符串、数值、布尔值和null,但不支持undefined对象:对象作为一种复杂的数据类型,表示是一组无序的键值对儿,值可以是简单值也可以复杂的数据数组:数组作为一种复杂的数据类型,表示是一组有序的列表儿,可以通过索引来获取引用的值,值可以是简单值,也可以复杂数据对象

A simple JSON example:

{    "name": "Jack",    "age": 30,    "isMan": true,    "school": {        "name": "Lonton University",        "location": "English"    }}

Very important: The properties of the JSON object ("name" in the example above) must be enclosed in double quotation marks.

In practice, it is often impossible to generate JSON objects by using single quotation marks or not using quotation marks to cause sequential death errors
Serialization of

 JSON被大家所认可,除了语法是JavaScript语法子集,更主要是可以直接eval序列化(但有一定风险,可能会执行一些恶意代码,如读cookie等)成可用的JS对象直接使用。ECMAScript 5对解析JSON进行规范,定义了全局对象JSON,支持的浏览器有:IE8+,Firefox 3.5+,Safari4+,Chrome和Opera 10.5+; JSON对象提供两个方法:stringify()和parse(),stringify()是将JSON对象转成字符串,而parse()则是将符合规范的字符串转成可用JSON对象。

Stringify ()

Json.stringify (value [, Replacer] [, space]) Value:json object. Replacer: Filter (optional), can be a method or an array, for filtering operations, if it is an array, the serialized string contains only the JSON attribute and property value of the value specified in the array, and if it is a method, the method passes Key,value,  The JSON object can be returned after each property value and value is processed. Space: Whether indentation is preserved, not persisted by default, and all line breaks are deleted. 1. If omitted, then the displayed value does not have a separator character. Direct output to 2. If it is a number, then it is defined to indent a few characters, of course, if it is greater than 10, the maximum value is 10.3. If there are some escape characters, such as "\ T", which means carriage return, then it is a carriage return per line. 4. If it is just a string, OK, append the strings to the output value in each row.   Of course, the maximum length is also 10 characters. Example: var student = new Object (); Student.name = "Lanny"; Student.age = "25"; student.location = "China"; var json = json.stringify (student); alert (JSON); {' name ': ' Lanny ', ' age ': ' + ', ' location ': ' China '} using array filtering, reserved only: Name and Locaitonvar json = json.stringify (student,["name" , "location"]), alert (JSON),//{"name": "Lanny", "Location": "China"} using function filtering, processing for the name attribute value separately, before output: "My name is:" var json = Json.stringify (student, function (key, value) {switch (key) {case ' name ': Return ' my name is ' + Val        Ue    Default:return value; }}); alert (JSON);//{' name ': ' My name is Lanny ', ' age ': ' + ', ' location ': ' China '}  Using indentation, indent 4 blanks: var json = json.stringify (student,null,4); alert (JSON);//Returns the result as follows: {"name": "Lanny", "Age": "+", "Loc ation ":" China "}

Parse ()

Json.parse (text [, Reviver])

Parameters

      text :必需。 一个有效的 JSON 字符串。        reviver :可选。 一个转换结果的函数。 将为对象的每个成员调用此函数。 如果成员包含嵌套对象,则先于父对象转换嵌套对象。 对于每个成员,会发生以下情况:如果 reviver 返回一个有效值,则成员值将替换为转换后的值。如果 reviver 返回它接收的相同值,则不修改成员值。如果 reviver 返回 null 或 undefined,则删除成员。

Return value: An object or an array

Example:

var jsontext = ‘{"firstname":"Jesper","surname":"Aaberg","phone":["555-0100","555-0120"]}‘; var contact = JSON.parse(jsontext);

The following example shows how to use Json.stringify to convert an array to a JSON string, and then use Json.parse to restore the string to a group.

var arr = ["a", "b", "c"];var str = JSON.stringify(arr);document.write(str);document.write ("<br/>");var newArr = JSON.parse(str);while (newArr.length > 0) {    document.write(newArr.pop() + "<br/>");}// Output:var arr = ["a", "b", "c"];var str = JSON.stringify(arr);document.write(str);document.write ("<br/>");var newArr = JSON.parse(str);while (newArr.length > 0) {    document.write(newArr.pop() + "<br/>");}// Output:["a","b","c"]cba

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

About the JSON in JS

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.