json - 列出JSON的所有键和值

假设我有一些像下面这样的 JSON:

{
"items":
    {
        "item":
            [
                {
                    "id": "0001",
                    "type": "donut",
                    "name": "Cake",
                    "ppu": 0.55,
                    "batters":
                        {
                            "batter":
                                [
                                    { "id": "1001", "type": "Regular" },
                                    { "id": "1002", "type": "Chocolate" },
                                    { "id": "1003", "type": "Blueberry" },
                                    { "id": "1004", "type": "Devil's Food" }
                                ]
                        },
                    "topping":
                        [
                            { "id": "5001", "type": "None" },
                            { "id": "5002", "type": "Glazed" },
                            { "id": "5005", "type": "Sugar" },
                            { "id": "5007", "type": "Powdered Sugar" },
                            { "id": "5006", "type": "Chocolate with Sprinkles" },
                            { "id": "5003", "type": "Chocolate" },
                            { "id": "5004", "type": "Maple" }
                        ]
                },

                ...

            ]
    }
}

我想要一个函数来返回制表符分隔数据列表(其中 -> 是一个制表符)。像这样:

items.item.length -> 1
items.item[0].id -> 0001
items.item[0].type -> donut
items.item[0].name -> Cake
items.item[0].ppu -> 0.55
items.item[0].batters.batter.length -> 4
items.item[0].batters.batter[0].id -> 1001
items.item[0].batters.batter[0].type -> Regular
items.item[0].batters.batter[1].id -> 1002
items.item[0].batters.batter[1].type -> Chocolate
items.item[0].batters.batter[2].id -> 1003
items.item[0].batters.batter[2].type -> Blueberry
items.item[0].batters.batter[3].id -> 1004
items.item[0].batters.batter[3].type -> Devil's Food
items.item[0].topping.length -> 7
items.item[0].topping[0].id -> 5001
items.item[0].topping[0].type -> None
items.item[0].topping[0].id -> 5002
items.item[0].topping[0].type -> Glazed

...

我在想类似的东西

function json2txt(obj) {
var txt = '';
    for (var key in obj) {
       if (obj.hasOwnProperty(key)) {
          if ("object" == typeof(obj[key])) {
             json2txt(obj[key]);
          } else txt += obj + '\t' + obj[key] + '\r';
       }
    }
}

“糟糕!您的修改无法提交,因为:

您的帖子没有太多上下文来解释代码部分;请更清楚地解释您的情况。”

这也很令人沮丧。

最佳答案

您的递归函数走在了正确的轨道上。不过,您需要向该函数添加一个参数——它需要知道对象中当前点的路径。

此外,使用\n,而不是\r

var inputObject = {
    items: { 
        foo: [ 'apples', 'oranges', 'peaches' ],
        bar: 'baz',
        spam: 'eggs'
     }
};
function json2txt(obj, path)
{
    var txt = '';
    for (var key in obj)
    {
        if (obj.hasOwnProperty(key))
        {
            if ('object' == typeof(obj[key]))
            {
                txt += json2txt(obj[key], path + (path ? '.' : '') + key);
            } 
            else
            {
                txt += path + '.' + key + '\t' + obj[key] + '\n';
            }
        }
    }
    return txt;
}
json2txt(inputObject, '');

有趣的问题!

对于您的示例数据,我的代码给出:

items.item.0.id 0001
items.item.0.type   donut
items.item.0.name   Cake
items.item.0.ppu    0.55
items.item.0.batters.batter.0.id    1001
items.item.0.batters.batter.0.type  Regular
items.item.0.batters.batter.1.id    1002
items.item.0.batters.batter.1.type  Chocolate
items.item.0.batters.batter.2.id    1003
items.item.0.batters.batter.2.type  Blueberry
items.item.0.batters.batter.3.id    1004
items.item.0.batters.batter.3.type  Devil's Food
items.item.0.topping.0.id   5001
items.item.0.topping.0.type None
items.item.0.topping.1.id   5002
items.item.0.topping.1.type Glazed
items.item.0.topping.2.id   5005
items.item.0.topping.2.type Sugar
items.item.0.topping.3.id   5007
items.item.0.topping.3.type Powdered Sugar
items.item.0.topping.4.id   5006
items.item.0.topping.4.type Chocolate with Sprinkles
items.item.0.topping.5.id   5003
items.item.0.topping.5.type Chocolate
items.item.0.topping.6.id   5004
items.item.0.topping.6.type Maple

https://stackoverflow.com/questions/10221229/

相关文章:

visual-studio-2010 - 如何断开 VisualSVN 与我的解决方案的连接?

php - MongoDB PHP 认证

ms-access - Access : Truncation error when appendi

perl - Perl 中的非本地返回(从调用者返回)

asp.net-mvc - 如何在 mvc 中的 Homecontroller 内根据条件更改提交按

jquery - 我如何使用 jQuery 找到 parent ?

sql - Oracle CONNECT BY 分层查询中对父列的引用

winapi - 如何更新进度条?

r - 在 R 中从循环或 lapply 按名称调用列表

php - 清理 XSS 到 Textarea 的输出