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

JSON string and js object conversion

高洛峰
高洛峰Original
2016-11-18 11:40:261325browse

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"
    }
};

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"}}';

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


Statement:
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