Stringify string into JSON method

Source: Internet
Author: User

Reference Original: http://www.cnblogs.com/damonlan/

Http://www.jb51.net/article/29893.htm

The role of stringify is primarily to serialize objects (converted to JSON objects)

Syntax:
Json.stringify (value [, Replacer] [, space])

1.value: Required, the objects you enter, such as arrays, classes, and so on.

2.replacer: Optional; can be an array or a method

3.space: Delimiter

Case one: (only one Parameter object)

var student = new Object(); student.name = "Lanny" ; student.age = "25" ; student.location = "China" ; var json = JSON.stringify(student); alert(student);

Case two: (the first parameter array, the second parameter is an arrays, only the value of the first parameter is displayed)

var students = new Array() ; students[0] = "Lanny" ; students[1] = "dong" ; students[2] = "I love you" ; var stu = new Array(); stu[0] = "1" ; stu[1] = "2" ; var json = JSON.stringify(students,stu); alert(json);

Scenario Three: (first object, second array)

var student = new Object(); student.qq = "5485891512" ; student.name = "Lanny" ; student.age = 25; var stu = new Array(); stu[0] = "qq" ; stu[1] = "age" ; stu[2] = "Hi" ; //这个student对象里不存在。 var json = JSON.stringify(student,stu); alert(json);If the value in the second array exists in the first argument, then the first value is used as the key value if it does not exist, it is not displayed, similarly, the key value in the first parameter object does not exist in the second parameter array, and does not give a display

Situation four: The second parameter exists, and is the time of the method

var students = new Array() ; students[0] = "Lanny" ; students[1] = "dong" ; students[2] = "I love you" ; var json = JSON.stringify(students,switchUpper); function switchUpper(key, value) { return value.toString().toUpperCase(); } alert(json);

The second argument simply means that each item of the first function is handled in the function.

Stringify string into JSON method

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.