Differences between JSON. stringify () and JOSN. parse () methods, stringifyparse

Source: Internet
Author: User
Tags string to json

Differences between JSON. stringify () and JOSN. parse () methods, stringifyparse

Today, I finally figured out the two methods of JSON. stringify () and JSON. parse (). The following small series will be recorded here!

JSON. tringify (): converts a json data to a JSON string

JSON.stringify({uno:1,dos:2},null,'\t')"{"uno": 1,"dos": 2}"JSON.stringfy({uno:1,dos:2})JSON.stringify({uno:1,dos:2})"{"uno":1,"dos":2}"JSON.stringify({uno:1,dos:2},null,'\n')"{"uno": 1,"dos": 2}"JSON.stringify({uno:1,dos:2},null,'\b')"{"uno": 1,"dos": 2}"JSON.stringify({uno:1,dos:2},null,'\f')"{"uno": 1,"dos": 2}"

JSON. parse (): converts string to json.

JSON.parse('{"a":"a"}');Object {a: "a"} JSON.parse('{"p": 5}', function(k, v) {if (typeof v === 'number') {return v * 2; // return v * 2 for numbers}return v; // return everything else unchanged});// { p: 10 }

See JSON. parse () and JSON. stringify ()

1. parse is used to parse json objects from a string. For example

var str='{"name":"cpf","age":"23"}'

JSON. parse (str:

Object: age:"23"   name:"cpf"   _proto_:Object

Ps: Each attribute must be enclosed in double quotation marks ({}). Otherwise, an exception is thrown.

2. stringify is used to parse a string from an object, for example

var a={a:1,b:2}

JSON. stringify (:

“{“a”:1,"b":2}”

The above is the JSON file that we will introduce to you. stringify () and JOSN. the parse () method is different and I hope it will help you. If you have any questions, please leave a message and I will reply to you in a timely manner. Thank you very much for your support for the help House website!

Related Article

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.