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