js原生JSON转化方法

JSON.parse()

将JSON数据(字符串)转化为JavaScript对象/数组

JSON.parse()第二个参数

JSON.parse()第二个参数为选填,用于将转换后的每个成员变量都调用此函数。
- 以下为权限树json的转化代码
- 每个被转化的对象属性都会被遍历
- 被遍历的每一项都是一个js对象
- eg. 一个含有两个属性值得对象会被遍历三次
- eg. {name: "分析结果", checked: false, id: 32}

    function test() {
        console.log(this);
    }
    var obj1 = '[{"children":[{"children":[{"children":[{"children":[],"name":"查询","checked":false,"id":10014}],"name":"用户请求","checked":false,"id":31},{"children":[{"children":[],"name":"查询","checked":false,"id":10013}],"name":"分析结果","checked":false,"id":32}],"name":"数据查询","checked":false,"id":3},{"children":[{"children":[],"name":"流量分析","checked":false,"id":41},{"children":[],"name":"攻击分析","checked":false,"id":42}],"name":"数据分析","checked":false,"id":4},{"children":[{"children":[{"children":[],"name":"查询","checked":false,"id":9},{"children":[{"children":[],"name":"保存","checked":false,"id":44}],"name":"新增页面","checked":false,"id":43},{"children":[{"children":[],"name":"保存","checked":false,"id":46}],"name":"修改页面","checked":false,"id":45},{"children":[],"name":"删除","checked":false,"id":52},{"children":[],"name":"冻结","checked":false,"id":231},{"children":[],"name":"解冻","checked":false,"id":232}],"name":"角色管理","checked":false,"id":8},{"children":[{"children":[],"name":"查询","checked":false,"id":71},{"children":[],"name":"修改","checked":false,"id":72},{"children":[],"name":"删除","checked":false,"id":73},{"children":[],"name":"新增","checked":false,"id":74}],"name":"权限管理","checked":false,"id":70},{"children":[{"children":[],"name":"查询","checked":false,"id":135},{"children":[{"children":[],"name":"保存","checked":false,"id":139}],"name":"修改页面","checked":false,"id":138},{"children":[],"name":"冻结","checked":false,"id":149},{"children":[],"name":"解冻","checked":false,"id":150},{"children":[],"name":"删除","checked":false,"id":151},{"children":[],"name":"重置密码","checked":false,"id":3724},{"children":[{"children":[],"name":"保存","checked":false,"id":137}],"name":"新增页面","checked":false,"id":10000}],"name":"用户管理","checked":false,"id":134}],"name":"系统管理","checked":false,"id":4031}],"name":"导航菜单","checked":false,"id":1,"open":true}]';
    console.log(JSON.parse(obj1,test));

JSON.stringify()

将JavaScript对象转化为JSON数据(字符串)

JSON.stringify(value,replacer)第二个参数

可选。用于转换结果的函数或数组。
- 如果 replacer 为函数,则 JSON.stringify 将调用该函数。
- 函数的返回值将作为转化结果
- 如果 replacer 是一个数组,则仅转换该数组中具有键值的成员。

function test(){
console.log(this);
    return null;
}
var obj = { "name":"runoob", "alexa":10000, "site":"inter"};
var myJSON = JSON.stringify(obj,test);

以上代码中,转化结果为null。

JSON.stringify(value,replacer,space)第三个参数

可选,设置转化为json数据后缩进的空格格数(最大数值为10)

var obj = { "name":"runoob", "alexa":10000, "site":"inter"};
var myJSON = JSON.stringify(obj,null,9);
console.log(myJSON);

转化后的myJSON对象会缩进9个空格。效果如下:

{
         "name": "runoob",
         "alexa": 10000,
         "site": "inter"
}

angular内置JSON转化方法

  • angular.toJson() 等价于JSON.stringify(),将JSON数据转化为JavaScript对象
  • angular.fromJson() 等价于JSON.parse(),将JavaScript对象转化为JSON数据(字符串)

END

GitHub 加速计划 / js / json
41.72 K
6.61 K
下载
适用于现代 C++ 的 JSON。
最近提交(Master分支:1 个月前 )
960b763e 3 个月前
8c391e04 6 个月前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐