The difference between a JSON object and a JSON string

Source: Internet
Author: User
Tags string to json

JSON object

Sometimes the two concepts are confused when doing the project, especially when using SPRINGMVC, the background @requestbody accepts a JSON-formatted string, which must be a string.
Let's start with the JSON object, which begins with the concept of an object, which can be called by using the object property. For example:

var person={"name":"tom","sex":"男","age":"24"}//json对象 console.log(person.name);//在控制台输出tom alert(typeof(person));//object

The above is the JSON object. is a call to attribute in this way perosn.name. The third line of code is to see the type of person, which is the object type.

JSON string

strings, strings in JavaScript that we often say are enclosed in single or double quotes. So what is the concept of a JSON string?

 var b=‘{"name":"2323","sex":"afasdf","age":"6262"}‘;//json字符串 console.log(b);//{"name":"2323","sex":"afasdf","age":"6262"} alert(typeof(b));//string

The above is B is a string, is also a JSON string, the reason is called a JSON string, because the format of the string is in JSON format, so called the JSON string, the third line of code also matches the type string.

Conversion of JSON strings and JSON objects

JSON string to JSON object, call the Parse method:

var b=‘{"name":"2323","sex":"afasdf","age":"6262"}‘//json字符串var bToObject=JSON.parse(b);console.log(bToObject.name);//2323

JSON object to JSON string:

var a={"name":"tom","sex":"男","age":"24"}//json对象var aToString=JSON.stringify(a);console.log(aToString);//{"name":"tom","sex":"男","age":"24"}

The difference between a JSON object and a JSON string

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.