首页 > 解决方案 > Groovy 脚本在循环 JSON 时创建重复项

问题描述

我有两组 JSON。一个是请求 JSON,另一个是响应 JSON。我需要根据某个字段将请求 JSON 与响应 JSON 进行比较,如果找到匹配项,我需要合并请求和响应 json 中的数据,如果未找到匹配项,我仍然合并数据,但将某些字段留空/未找到丢失的。我已经创建了一个代码,但是在创建最终输出结构时我得到了重复。如果有人可以帮助我解决问题,那就太好了。

req_json:

{
  "val": [
    {
      "custNumber": "Z10000",
      "custID": "1000",
      "custName": "Jack",
      "custType": "Private"
    },
    {
      "custNumber": "Z20000",
      "custID": "2000",
      "custName": "Tina",
      "custType": "Private"
    },
    {
      "custNumber": "B31000",
      "custID": "3100",
      "custName": "ACME Holdings",
      "custType": "Business"
    }
  ]
}

resp_json:

{
  "SELECT_FROM_DB_response": {
    "row": [
      {
        "CUSTNO": "Z10000",
        "PRODUCTID": "P21000",
        "PRODUCTNAME": "KEYBOARD",
        "PRODUCTSTATUS": "Shipped",
      },
      {
        "CUSTNO": "Z20000",
        "PRODUCTID": "L53000",
        "PRODUCTNAME": "MOUSE",
        "PRODUCTSTATUS": "Processing",
      },
      {
        "CUSTNO": "B31000",
        "PRODUCTID": "N99500",
        "PRODUCTNAME": "MONITOR",
        "PRODUCTSTATUS": "Delivered",
      }
    ]
  }
}

我的代码如下所示。但是,当我执行它时,我会得到重复的输出。我需要解决这个问题,但到目前为止我没有运气。

常规代码:

def mReqOrders = new JsonSlurper().parseText(req_json)
def mRespOrders = new JsonSlurper().parseText(resp_json)
Set finalJsonSet = []

mReqOrders.val.each { reqOrder -> 
                        mRespOrders.SELECT_FROM_DB_response.row.each { respOrder ->
                        if (reqOrder.custNumber == respOrder.CUSTNO) {
                            
                            finalJsonSet << [
                                order_custNumber: respOrder.CUSTNO,
                                order_custID: reqOrder.custID,
                                order_custName: reqOrder.custName, 
                                order_custType: reqOrder.custType,
                                order_productID: respOrder.PRODUCTID,
                                order_productNAME: respOrder.PRODUCTNAME,
                                order_productSTATUS: respOrder.PRODUCTSTATUS
                                ]
                        }
                        else {
                                finalJsonSet << [
                                order_custNumber: respOrder.CUSTNO,
                                order_custID: reqOrder.custID,
                                order_custName: reqOrder.custName, 
                                order_custType: reqOrder.custType,
                                order_productID: "not found",
                                order_productNAME: "not found",
                                order_productSTATUS: "not found"
                                ]       
                        }                       
                    }                   
                }
        //Convert the Set to JSON Format    
        finalJson = JsonOutput.prettyPrint(JsonOutput.toJson(finalJsonSet))
        println (finalJson)

当前输出:

[
    {
        "order_custNumber": "Z10000",
        "order_custID": "1000",
        "order_custName": "Jack",
        "order_custType": "Private",
        "order_productID": "P21000",
        "order_productNAME": "KEYBOARD",
        "order_productSTATUS": "Shipped"
    },
    {
        "order_custNumber": "Z20000",
        "order_custID": "1000",
        "order_custName": "Jack",
        "order_custType": "Private",
        "order_productID": "not found",
        "order_productNAME": "not found",
        "order_productSTATUS": "not found"
    },
    {
        "order_custNumber": "B31000",
        "order_custID": "1000",
        "order_custName": "Jack",
        "order_custType": "Private",
        "order_productID": "not found",
        "order_productNAME": "not found",
        "order_productSTATUS": "not found"
    },
    {
        "order_custNumber": "Z10000",
        "order_custID": "2000",
        "order_custName": "Tina",
        "order_custType": "Private",
        "order_productID": "not found",
        "order_productNAME": "not found",
        "order_productSTATUS": "not found"
    },
    {
        "order_custNumber": "Z20000",
        "order_custID": "2000",
        "order_custName": "Tina",
        "order_custType": "Private",
        "order_productID": "L53000",
        "order_productNAME": "MOUSE",
        "order_productSTATUS": "Processing"
    },
    {
        "order_custNumber": "B31000",
        "order_custID": "2000",
        "order_custName": "Tina",
        "order_custType": "Private",
        "order_productID": "not found",
        "order_productNAME": "not found",
        "order_productSTATUS": "not found"
    },
    {
        "order_custNumber": "Z10000",
        "order_custID": "3100",
        "order_custName": "ACME Holdings",
        "order_custType": "Business",
        "order_productID": "not found",
        "order_productNAME": "not found",
        "order_productSTATUS": "not found"
    },
    {
        "order_custNumber": "Z20000",
        "order_custID": "3100",
        "order_custName": "ACME Holdings",
        "order_custType": "Business",
        "order_productID": "not found",
        "order_productNAME": "not found",
        "order_productSTATUS": "not found"
    },
    {
        "order_custNumber": "B31000",
        "order_custID": "3100",
        "order_custName": "ACME Holdings",
        "order_custType": "Business",
        "order_productID": "N99500",
        "order_productNAME": "MONITOR",
        "order_productSTATUS": "Delivered"
    }
]

预期输出:

[
    {
        "order_custNumber": "Z10000",
        "order_custID": "1000",
        "order_custName": "Jack",
        "order_custType": "Private",
        "order_productID": "P21000",
        "order_productNAME": "KEYBOARD",
        "order_productSTATUS": "Shipped"
    },
    {
        "order_custNumber": "Z20000",
        "order_custID": "2000",
        "order_custName": "Tina",
        "order_custType": "Private",
        "order_productID": "L53000",
        "order_productNAME": "MOUSE",
        "order_productSTATUS": "Processing"
    },
    {
        "order_custNumber": "B31000",
        "order_custID": "3100",
        "order_custName": "ACME Holdings",
        "order_custType": "Business",
        "order_productID": "N99500",
        "order_productNAME": "MONITOR",
        "order_productSTATUS": "Delivered"
    }
]

标签: jsongrailsgroovygroovy-console

解决方案


尝试“ find ”而不是“ each ”来检查客户编号是否匹配

mReqOrders.val.each { reqOrder -> 

                    def matchedRow = mRespOrders.SELECT_FROM_DB_response.row.find {respOrder -> reqOrder.custNumber == respOrder.CUSTNO}
                   
                    if (matchedRow) {

                        finalJsonSet << [
                            order_custNumber: respOrder.CUSTNO,
                            order_custID: reqOrder.custID,
                            order_custName: reqOrder.custName, 
                            order_custType: reqOrder.custType,
                            order_productID: respOrder.PRODUCTID,
                            order_productNAME: respOrder.PRODUCTNAME,
                            order_productSTATUS: respOrder.PRODUCTSTATUS
                            ]
                    } else {

                        finalJsonSet << [
                            order_custNumber: respOrder.CUSTNO,
                            order_custID: reqOrder.custID,
                            order_custName: reqOrder.custName, 
                            order_custType: reqOrder.custType,
                            order_productID: "not found",
                            order_productNAME: "not found",
                            order_productSTATUS: "not found"
                            ]  
                    }            
            }

推荐阅读