欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  web前端

JSON字符串和js对象转换

程序员文章站 2022-04-15 12:06:03
...

js数据类型:

字符串、数字、布尔、数组、对象、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
相关标签: json