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 - AngularJS ng-repeat 列表动态构建,$index 始终为零

javascript - 使用 ng-file-upload 时工厂未定义

javascript - 解析 JSON 数据在第一个结果前面给出 "undefined"

javascript如何创建引用

javascript - 实现芯片初始化

javascript - 隐藏输入需要文本

javascript - RxJS - forkJoin 何时触发?

javascript - 在 ng-repeat 中从 Controller 重置 ng-model

javascript - xml 到 json 映射挑战

ios - 开发iOS应用程序时从服务器获取JSON数据时包含奇怪的字符