将JSON对象转换为另一个对象-对其进行格式化 [英] Transform JSON Object to another - Format It

查看:47
本文介绍了将JSON对象转换为另一个对象-对其进行格式化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将json格式的输出转换为另一个.我该怎么做?

i want to transform a json formated output to another. How i can do this ?

示例:旧的json

"data": 
[
    {
        "id" : "e49e183e-9325-4e62-8eda-7e63fb7cdbbd",
        "name" : "test"
    },
    {
        "id" : "ac310894-d808-447b-a189-d07edb7f6dd7",
        "name" : "test2"
    }
]

我想要不带牙套的新Json只像这样带括号

New Json which i want without braces only like this with bracket

"aaData": 
[ 
    [
        "e49e183e-9325-4e62-8eda-7e63fb7cdbbd","test"
    ],
    [
        "ac310894-d808-447b-a189-d07edb7f6dd7","test2"
    ]
] 

推荐答案

您可以遍历所有项目并将其推入新对象:

You could just loop through the items and push them into a new object:

var len = old.data.length,
    newData = {aaData:[]},
    i;

for ( i=0; i < len; i+=1 ) {
    newData.aaData.push( [ old.data[ i ].id, old.data[ i ].name] );   
}

示例: https://jsfiddle.net/q2Jzb/1/

您大概会将它们传递给DataTables(使用名称aaData),请注意,DataTables 将对象作为配置,它与JSON不同.

You are presumably passing these to DataTables (as you use the name aaData), note that DataTables takes an object as the configuration, it's not the same as JSON.

这篇关于将JSON对象转换为另一个对象-对其进行格式化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆