JSON从子对象获取父对象 [英] JSON Get parent object from child object

查看:349
本文介绍了JSON从子对象获取父对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果 brand_id =='983',我如何获得折扣值.

示例JSON:

{
     "prods": [
               {
            "info": {
                  "rate": 100
                    },
            "grocery": [
                     {
                      "brand": "A",
                      "brand_id": "983"
                     },
                     {
                      "brand": "B",
                      "brand_id": "253"
                     }
                     ],
             "discount": "20"
         }
     ]
}

到目前为止,我一直尝试的是

What I have tried till now is

$.prods[*].grocery[?(@.brand_id=='983')]

这将向我返回匹配对象的列表/数组.但是我无法穿越到树上.有什么帮助吗?

This returns me list/array of matched objects. But I am not able to traverse back into the tree. Any help on this?

推荐答案

实际上,JSONPath并不是很擅长,所以我用自己的小型库解决了此类问题;因此,这是您的示例的小提琴:

Indeed, JSONPath isn't very good at that, so I tackled this kind of problem with my own small library; so, here's a fiddle for your example:

https://jsfiddle.net/YSharpLanguage/j9oetwnn/3

其中:

var products = {
     "prods": [
        {
            "info": {
                  "rate": 85
                    },
            "grocery": [
                     {
                      "brand": "C",
                      "brand_id": "984"
                     },
                     {
                      "brand": "D",
                      "brand_id": "254"
                     }
                     ],
             "discount": "15"
        },
        {
            "info": {
                  "rate": 100
                    },
            "grocery": [
                     {
                      "brand": "A",
                      "brand_id": "983"
                     },
                     {
                      "brand": "B",
                      "brand_id": "253"
                     }
                     ],
             "discount": "20"
         }
     ]
};

function GroceryItem(obj) {
  return (typeof obj.brand === "string") && (typeof obj.brand_id === "string");
}

    // last parameter set to "true", to grab all the "GroceryItem" instances
    // at any depth:
var itemsAndDiscounts = [ products ].nodeset(GroceryItem, true).
    map(
      function(node) {
        var item = node.value, // node.value: the current "GroceryItem" (aka "$.prods[*].grocery[*]")

            discount = node.parent. // node.parent: the array of "GroceryItem" (aka "$.prods[*].grocery")
                       parent. // node.parent.parent: the product (aka "$.prods[*]")
                       discount; // node.parent.parent.discount: the product discount

        // finally, project into an easy-to-filter form:
        return { id: item.brand_id, discount: discount };
      }
    ),
    discountOfItem983;

discountOfItem983 = itemsAndDiscounts.
  filter
  (
    function(mapped) {
      return mapped.id === "983";
    }
  )
  [0].discount;

console.log("All items and discounts: " + JSON.stringify(itemsAndDiscounts, null, 2));

console.log("Discount of #983: " + discountOfItem983);

给予:

All items and discounts: [
  {
    "id": "984",
    "discount": "15"
  },
  {
    "id": "254",
    "discount": "15"
  },
  {
    "id": "983",
    "discount": "20"
  },
  {
    "id": "253",
    "discount": "20"
  }
]
Discount of #983: 20

以下是其他示例/用例:

Here are other examples / use cases:

JSON转换,已重新审查(类似XSLT)

(在: https://jsfiddle.net/YSharpLanguage/kj9pk8oz/10 )

超轻量级JSON到JSON转换

(在: https://jsfiddle.net/YSharpLanguage/ppfmmu15/10 )

XSLT 3.0 REC第14.4节示例:基于公共值对节点进行分组

(在: http://jsfiddle.net/YSharpLanguage/8bqcd0ey/1 )

Cf. https://www.w3.org/TR/xslt-30/#grouping -示例

JSONiq用例第1.1.2节. JSON的分组查询

(在: https://jsfiddle.net/YSharpLanguage/hvo24hmk/3 )

Cf. http://jsoniq.org/docs/JSONiq-usecases/html -single/index.html#jsongrouping

'希望这会有所帮助,

这篇关于JSON从子对象获取父对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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