本文介绍了如何通过Jayway JsonPath将带有嵌套列表的json扁平化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我需要根据配置而不是硬代码来处理一些json结果.

Currently I need to process some json results based on configuration but not hard code.

例如,使用以下json

For example, with the json as follows

{
    data: [{
        orderNo: "CG8310150",
        details: [{
            skuId: 4384,
            amount: 2
        }, {
            skuId: 4632,
            amount: 5
        }]
    }, {
        orderNo: "CG8310151",
        details: [{
            skuId: 4384,
            amount: 3
        }]
    }]
}

我想要的结果如下

[{
    orderNo: "CG8310150",
    skuId: 4384,
    amount: 2
}, {
    orderNo: "CG8310150",
    skuId: 4632,
    amount: 5
}, {
    orderNo: "CG8310151",
    skuId: 4384,
    amount: 3
}]

如果有人使用Jayway JsonPath解决方案,或者对其他工具有任何建议,请告诉我.

If anyone has the solution with Jayway JsonPath, or has any suggestion of other tools, please let me known.

感谢您的帮助!

推荐答案

您可以使用JsonPath从该JSON投影结果.例如:

You can project results from that JSON using JsonPath. For example:

  • $['data'][*]['orderNo']返回:

["CG8310150","CG8310151"]

  • $['data'][*]['details'][*]['skuId', 'amount']返回:

    [{"skuId":4384,"amount":2},{"skuId":4632,"amount":5},{"skuId":4384,"amount":3}]
    

  • 但是您不能一次通过JsonPath combine 这两个表达式,因此不能使用JsonPath返回目标输出.

    But you cannot combine both of those expressions in one pass through JsonPath so you cannot use JsonPath to return your target output.

    这篇关于如何通过Jayway JsonPath将带有嵌套列表的json扁平化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    10-28 05:53