javascript - 在 AngularJS 中访问具有未知名称的嵌套 JSON 值

标签 javascript angularjs json

如果事先不知道值名称,我如何访问嵌套 JSON 中的值。

即在下面的 JSON 中,我需要访问糖果选项的名称以将它们添加到 SQLite 表中。但是我事先并不知道糖果的类型以及它们的每个糖果选项名称。

JSON 示例可能如下所示,其中可以有任意数量的糖果选项,每个选项都有自己的数量。

JSON

[{
    "sweet": "sweets",
    "sweets": 
    [{
        "idsweets": "1",
        "sweetsdesc": "Choocolate Chip"
    }, {
        "idsweets": "2",
        "sweetsdesc": "Mint"
    }],
    "drink": "drinks",
    "drinks": 
    [{
        "iddrinks": "1",
        "drinksdesc": "Coke"
    }, {
        "iddrinks": "2",
        "drinksdesc": "Pepsi"
    }, {
        "iddrinks": "3",
        "drinksdesc": "Fanta"
    }, {
        "iddrinks": "4",
        "drinksdesc": "Dr. Pepper"
    }, {
        "iddrinks": "5",
        "drinksdesc": "Powerade"
    }],
    "crisp": "crisps",
    "crisps": 
    [{
        "idcrisps": "1",
        "crispsdesc": "Salt"
    }, {
        "idcrisps": "2",
        "crispsdesc": "Vinegar"
    }, {
        "idcrisps": "3",
        "crispsdesc": "Cheese and Onion"
    }, {
        "idcrisps": "4",
        "crispsdesc": "Pepper"
    }, {
        "idcrisps": "5",
        "crispsdesc": "Chilli"
    }, {
        "idcrisps": "6",
        "crispsdesc": "Other"
    }]
}]

为了将每个糖果选项插入数据库,我尝试了以下操作,但没有成功。

function goInsertValueOptionsList()
{
    db.transaction(insertValueOptionsList, errorCB, successCB);
}

function insertValueOptionsList(tx)
{
    angular.forEach($scope.optionValuesAPIData, function (value, key) // A variable to hold the confectionary JSON
    {
        alert("Value Options - Value: " + value); // Prints the JSON [object Object]

        for (var description in value)
        {
            // Get the Column name
            alert("Value Options - Description (Column name): " + description);
            // Get the Column value
            alert("Value Options - Description (Column Value): " + value[description]);

            // Inner loop for nested JSON objects
            angular.forEach(value, function (v, k)
            {
                // Get the Column name - try to access e.g. idcrisps
                alert("V Options - Description (Column name): " + v); // Not working
                // Get the Column value
                alert("V Options - Description (Column Value): " + k); // Not working

                // Use v and k to insert into database
            });
        }
    });   
}

最佳答案

查看您提供的内容,您可以使用如下代码遍历所有这些对象:

data.forEach(function(dataset) // A variable to hold the confectionary JSON
{

    Object.keys(dataset).forEach(function(key) 
    {

        // Inner loop for nested JSON objects
        if(Array.isArray(dataset[key])) {
          dataset[key].forEach(function(item)
          {
              console.log(item)

              // Use v and k to insert into database
          });
        } else {
          console.log(dataset[key])
        }

    })

});  

http://plnkr.co/edit/4x4uKOqmqxZcDS2DPgPM?p=preview

关于javascript - 在 AngularJS 中访问具有未知名称的嵌套 JSON 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38274907/

相关文章:

javascript - 根据超时条件有条件地调用 jquery 事件

javascript - 如何从浏览器安装脚本到手机并运行?

javascript - 如何合并数组内重复对象的属性

javascript - 如何使用 Angular 服务从 API 将数据获取到表中?

angularjs - 如何为 AngularStrap datetimepicker 显示 'invalid date' 验证消息

javascript - 这里是 Node.js 的新手。如何正确从 subreddit 顶部获取 JSON?

javascript - 动态右 Pane 样式问题

javascript - 发出函数作为参数

ruby-on-rails - 将文件发送到 Rails JSON API

php - 使用 JSON 将 mysql 数据加载到 Highcharts 折线图