【问题标题】:Searching through JSON object搜索 JSON 对象
【发布时间】:2015-02-13 12:11:05
【问题描述】:

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

{

 "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});
           // }
        });
    };

编辑二号

JSON 结构发生了变化

【问题讨论】:

  • 请分享您到目前为止所尝试的内容。
  • 我认为你必须使用`portfolio.conn[1].product-2.attributes`如果工作而不是问我我会评论它,因为你必须接受这些答案
  • 您可以尝试使用 JPath :s-anand.net/blog/jpath-xpath-for-javascript
  • 嘿 Gunjan - 我试过了,我已经在使用 allProducts.data.portfolio[0].conn 进入产品列表。但我不知道如何通过 conn 搜索“product-2”并将其属性加载到数组中

标签: javascript json angularjs


【解决方案1】:

使用过滤器来解构数组。

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

过滤器表达式本质上说,返回数组中的第一个对象,其属性为“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>

【讨论】:

    【解决方案2】:

    好吧,如果您不知道 product-2 将如何嵌套并且您实际上需要搜索它,那么您需要进行 recursive 搜索。递归函数是调用自身的函数。

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

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

    【讨论】:

    • 谢谢 - 我知道 product-2 将如何嵌套,如上所述。这是否意味着我不应该进行递归搜索?
    • 正如 Gunjan Patel 在评论中所说的那样。
    【解决方案3】:

    我认为你的 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/

    【讨论】:

      猜你喜欢
      • 2017-07-04
      • 2012-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-23
      • 2012-09-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多