JavaScript操作JSON实用方法


1、JSON字符串转化JSON对象

let JSONString = '{"userName": "半晨", "password": 20211117}';
console.log(JSON.parse(JSONString));
// {userName: "半晨", password: 20211117}

2、JSON对象转化JSON字符串

let JSONObject = {
    
     userName: '半晨', password: '20211117' };
console.log(JSON.stringify(JSONObject));
// {"userName":"半晨","password":"20211117"}

3、JSON字符串的输出美化

let beautify = [{
    
     userName: '半晨', password: '20211117' }];
console.log(JSON.stringify(beautify, undefined, 2));
// [
//   {
    
    
//     "userName": "半晨",
//     "password": "20211117"
//   }
//  ]

4、相关文章链接

CSDN-原文

Guess you like

Origin blog.csdn.net/weixin_51157081/article/details/121371664