You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ruby中获取位置不固定的SubTotalLineDetail类型JSON元素值的最优方法

Ruby提取动态位置的SubTotalLineDetail行的amount值

给定如下API返回的JSON数据:

[
    {
        "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
    }
]

需求是获取detail_type为"SubTotalLineDetail"的行对应的amount值(本例为1005.0),由于该行在数组中的位置不固定,不能依赖last方法取最后一个元素。

解决方案

利用Ruby数组的find方法,通过detail_type字段精准匹配目标行,示例代码如下:

# 假设line_items是已经解析完成的Ruby数组(从API返回的JSON转换而来)
sub_total_line = line_items.find { |line| line["detail_type"] == "SubTotalLineDetail" }
# 获取amount值,同时处理找不到目标行的边界情况
sub_total_amount = sub_total_line ? sub_total_line["amount"] : nil
# 若需要将字符串类型的金额转为数值类型
sub_total_amount = sub_total_amount.to_f if sub_total_amount

说明

  • find方法会遍历数组,返回第一个满足条件的元素,完全不受元素位置影响;
  • 如果存在多个detail_type为SubTotalLineDetail的行,可改用select方法获取所有匹配行后再按需处理;
  • 代码中加入了空值判断,避免找不到目标行时触发异常。

内容的提问来源于stack exchange,提问作者Will DeBernardi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 20:27:14