如何访问可能并不总是位于同一位置的某个 JSON 元素?

How can I access a certain JSON element that may not always be in the same location?

提问人:Will DeBernardi 提问时间:3/3/2023 更新时间:3/14/2023 访问量:38

问:

鉴于以下从 API 返回的 JSON,Ruby 中从数组元素中检索元素的最佳方式是什么?在本例中,值为 1005。value"detail_type": "SubTotalLineDetail"

问题在于小计行项目位置没有固定为始终是倒数第二个行项目,它通常位于最后一个位置,但是,如示例中所示,这可能会发生变化,因此我的尝试并不总是有效。i.sub_total = invoice[:line_items].last[:amount]

[
    {
        "id": "1",
        "line_num": 1,
        "description": "Custom Design",
        "amount": "300.0",
        "detail_type": "SalesItemLineDetail",
        "sales_line_item_detail": {
            "item_ref": {
                "value": "4",
                "name": "Design",
                "type": null
            },
            "class_ref": null,
            "unit_price": "75.0",
            "rate_percent": null,
            "price_level_ref": null,
            "quantity": "4.0",
            "tax_code_ref": {
                "value": "NON",
                "name": null,
                "type": null
            },
            "service_date": null,
            "tax_inclusive_amount": null
        },
        "sub_total_line_detail": null,
        "payment_line_detail": null,
        "discount_line_detail": null,
        "group_line_detail": null,
        "description_line_detail": null
    },
    {
        "id": null,
        "line_num": null,
        "description": null,
        "amount": "1005.0",
        "detail_type": "SubTotalLineDetail",
        "sales_line_item_detail": null,
        "sub_total_line_detail": {
            "item_ref": null,
            "class_ref": null,
            "unit_price": null,
            "quantity": null,
            "tax_code_ref": null
        },
        "payment_line_detail": null,
        "discount_line_detail": null,
        "group_line_detail": null,
        "description_line_detail": null
    },
    {
        "id": null,
        "line_num": null,
        "description": null,
        "amount": "50.25",
        "detail_type": "DiscountLineDetail",
        "sales_line_item_detail": null,
        "sub_total_line_detail": null,
        "payment_line_detail": null,
        "discount_line_detail": {
            "discount_ref": null,
            "percent_based": true,
            "discount_percent": "5.0",
            "discount_account_ref": {
                "value": "86",
                "name": "Discounts given",
                "type": null
            },
            "class_ref": null,
            "tax_code_ref": null
        },
        "group_line_detail": null,
        "description_line_detail": null
    }
]
JSON Ruby-on-Rails 红宝石

评论

1赞 engineersmnky 3/3/2023
您可以简单地迭代以找到 .鉴于您当前的 ,HashArrayaa.find {|h| h["detail_type"] == "SubTotalLineDetail" }["amount"]
0赞 Will DeBernardi 3/3/2023
@engineersmnky 是的,工作得很好,因为没有早点想到这一点而感到愚蠢。谢谢!

答:

0赞 Crazy Cat 3/14/2023 #1
array.filter_map {|x| x[:amount].to_f if x[:detail_type].eql?('SubTotalLineDetail') }

如果你想对它求和

array.filter_map {|x| x[:amount].to_f if x[:detail_type].eql?('SubTotalLineDetail') }.sum