列出JSON的所有键和值 [英] List all keys and values of JSON

查看:95
本文介绍了列出JSON的所有键和值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一些类似以下的JSON:

Say I have some JSON like the following:

{
"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" }
                        ]
                },

                ...

            ]
    }
}

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

I'd like a function to return a list of tab delimited data (where -> is a tab). Something like this:

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';
       }
    }
}

糟糕!您的修改无法提交,原因是:

"Oops! Your edit couldn't be submitted because:

您的帖子没有太多内容可以解释代码部分;请更清楚地说明您的情况."

Your post does not have much context to explain the code sections; please explain your scenario more clearly."

这也很令人沮丧.

推荐答案

使用递归函数,您正处于正确的轨道上.不过,您需要在该函数中添加一个参数-它需要知道对象中当前点的路径.

You're on the right track with the recursive function. You'll need to add an argument to that function, though—it needs to know the path to the current point in the object.

此外,请使用\n,而不要使用\r.

Also, use \n, not \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, '');

好玩的问题!

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

For your example data, my code gives:

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

这篇关于列出JSON的所有键和值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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