搜索JSON对象

时间:2015-02-13 12:11:05

标签: javascript json angularjs

我正在尝试搜索json对象以选择一些值。例如,我有一个变量,其值为' product-2 '我想查看json对象并返回属性数组' product-2 '

{

 "attributes": [     
...
 ],
"portfolio": [
{
    "conn": [
        {

                "product": "product-1",
                "description": "Description in here",
                 "attributes": [
                    "OriginPostcode",
                    "Size",
                    "Bandwidth"
                ],

        },
        {

                "product": "product-2",
                "description": "Description in here"
                 "attributes": [
                    "OriginPostcode",
                    "Size",
                    "Bandwidth"
                ],
            }

    ]

}
]

有人能告诉我如何实现这一目标吗?谢谢

修改

根据Pramods的要求 - 我正在使用以下js(虽然我确实错了)

$scope.productAttributes = [];
    $scope.getProductDetails = function (product_id) {
        console.log(product_id);
        //search trough json
        angular.forEach($scope.listOfProducts.product_id, function(value, key) {

           // I was thinking I could loop through the json and when I find the matching product, then push its attributes into an array?
           // if (key === enteredValue) {
             //   $scope.productAttributes.push({atribute: key});
           // }
        });
    };

编辑第2期

JSON结构已更改

3 个答案:

答案 0 :(得分:1)

使用过滤器来解构数组。

在我的示例中,我在Controller中使用了一个过滤器。这可能应该在服务或视图中完成。为简洁起见,我在控制器中使用了滤波器。

过滤器表达式基本上是说,返回数组中的第一个对象,其中包含属性' product'那是' product-2'



var app = angular.module('app', []).controller('MyController', MyController);

MyController.$inject = ['$filter'];
function MyController($filter) {

  var data = [
        {

                "product": "product-1",
                "description": "Description in here",
                 "attributes": [
                    "OriginPostcode",
                    "Size",
                    "Bandwidth"
                ],

        },
        {

                "product": "product-2",
                "description": "Description in here",
                 "attributes": [
                    "OriginPostcode",
                    "Size",
                    "Bandwidth"
                ],
            }
    ]
  
    
    this.product = $filter('filter')(data, {product: "product-2"})[0];
  
}

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app">
  <div ng-controller="MyController as vm">
    Product-2: {{vm.product}}
  </div>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

好吧,如果你不知道产品2将如何嵌套而你实际上需要搜索它,那么你需要进行递归搜索。递归函数是一个自我调用的函数。

这意味着你遍历每个键,如果键是一个对象,也可以在该键上调用递归函数,直到找到你想要的键。

这是一个类似的问题,提供了一些算法,用于在JavaScript中对JSON结构进行递归搜索:traversing through JSON string to inner levels using recursive function

答案 2 :(得分:0)

我认为您的JSON错了,所以请更正

更正JSON

{
    "attributes": [],
"portfolio": [
    {
    "conn": [
        {
            "product-1": {
                "label": "product-1",
                "description": "Description in here",
        "attributes": [
                    "OriginPostcode",
                    "Size",
                    "Bandwidth"
                ],
            }
        },
        {
            "product-2": {
                "label": "product-2",
                "description": "Description in here",
         "attributes": [
                    "OriginPostcode",
                    "Size",
                    "Bandwidth"
                ],
            }
        }
        ]
    }
]
}

要解析json以上并返回以下产品信息是代码

$(document).ready(function() {
    $.each(dict['portfolio'][0], function(key, list){
        $.each(list, function(index, value){
            $.each(value, function(product, info){
                if (product == "product-2"){
                    answer = {}
                    answer[product] = info;
                    return JSON.stringify(answer);
                }
            });
        });
    }); 
});

小提琴链接: -

  

http://fiddle.jshell.net/2t8uknkc/