Home > Web Front-end > JS Tutorial > JSON string and js object conversion

JSON string and js object conversion

高洛峰
Release: 2016-11-18 11:40:26
Original
1339 people have browsed it

js data types:

String, Number, Boolean, Array, Object, Null, Undefined

js Object

var data = {
    "name":"yfs",
    "age":28,
    "boy":true,
    "language":["chinese", "english"],
    "address":{
        "provice":"shanxi",
        "city":"xian"
    }
};
Copy after login

JSON.stringify();

var jsonString = JSON.stringify(data);
console.log(jsonString);
console.log(typeof(jsonString));//结果:
 {"name":"yfs","age":28,"boy":true,"language":["chinese","english"],"address":{"p
rovice":"shanxi","city":"xian"}}//类型:stringvar a = '{"name":"yfs","age":28,"boy":true,"language":["chinese","english"],"address":{"p
rovice":"shanxi","city":"xian"}}';
Copy after login

JSON.parse();

var jsObject = JSON.parse(jsonString);console.log(jsObject);console.log(typeof(jsObject));//结果:{ name: 'yfs',
  age: 28,
  boy: true,
  language: [ 'chinese', 'english' ],
  address: { provice: 'shanxi', city: 'xian' } }  //类型:object
Copy after login


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template