JavaScript 或 Ruby。重新格式化 JSON 对象

发布于 2024-11-02 08:29:52 字数 1479 浏览 0 评论 0原文

我需要将这个 JSON 对象:

[
    [
        "[email protected]"
    ],
    [
        "[email protected]"
    ],
    [
        "[email protected]"
    ],
    [
        "[email protected]"
    ]
]

变成这样:

{
    "data": [
        {
            "email": "[email protected]"
        },
        {
            "email": "[email protected]"
        },
        {
            "email": "[email protected]"
        },
        {
            "email": "[email protected]"
        }
] }

这是如何完成的?

I need to turn this JSON object:

[
    [
        "[email protected]"
    ],
    [
        "[email protected]"
    ],
    [
        "[email protected]"
    ],
    [
        "[email protected]"
    ]
]

Into this:

{
    "data": [
        {
            "email": "[email protected]"
        },
        {
            "email": "[email protected]"
        },
        {
            "email": "[email protected]"
        },
        {
            "email": "[email protected]"
        }
] }

How is this done?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

凉薄对峙 2024-11-09 08:29:52

嗯,这实际上只是一个数组的数组,但这不是重点。只需循环数组数组并将相关数据推送到对象中的新数组即可:

var my_new_object = {};
my_new_object['data'] = [];

for (var i = 0; i < your_array.length; i++) {
  my_new_object.data.push({"email": your_array[i][0]});
}

工作演示

Well, that's really just an array of arrays, but that's besides the point. Just loop through the array of arrays and push the relevant data onto the new array in your object:

var my_new_object = {};
my_new_object['data'] = [];

for (var i = 0; i < your_array.length; i++) {
  my_new_object.data.push({"email": your_array[i][0]});
}

Working demo.

幸福%小乖 2024-11-09 08:29:52

JavaScript:

var original = [
[
    "[email protected]"
],
[
    "[email protected]"
],
[
    "[email protected]"
],
[
    "[email protected]"
]
];

var output = {};
output.data = new Array();
for(var i=0;i<original.length;i++){
    var o = new Object();
    o.email = original[i][0];
    output.data.push(o);
}

Javascript:

var original = [
[
    "[email protected]"
],
[
    "[email protected]"
],
[
    "[email protected]"
],
[
    "[email protected]"
]
];

var output = {};
output.data = new Array();
for(var i=0;i<original.length;i++){
    var o = new Object();
    o.email = original[i][0];
    output.data.push(o);
}
想你的星星会说话 2024-11-09 08:29:52

你可以在这里测试它:
http://jsfiddle.net/v3fnk/

var data=[["[email protected]"],["[email protected]"],["[email protected]"],["[email protected]"]];

for (var i in data) {
    for (var x in data[i]) {
        $("#info").append(data[i][x] + '<br/>');
    }

}

you can test it here:
http://jsfiddle.net/v3fnk/

var data=[["[email protected]"],["[email protected]"],["[email protected]"],["[email protected]"]];

for (var i in data) {
    for (var x in data[i]) {
        $("#info").append(data[i][x] + '<br/>');
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文