首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >groovy:LinkedHash:缺失属性

groovy:LinkedHash:缺失属性
EN

Stack Overflow用户
提问于 2022-10-25 05:54:33
回答 1查看 34关注 0票数 0

我很难从子数组customerNumber和allPersons中删除键‘allAddresses’。我的要求是,在下面的有效负载键中,customerNumber应该只出现在父数组CustomerMaster中。customerNumber的所有其他外观都应删除。

对于下面的代码,我得到了错误groovy.lang.MissingPropertyException: No这样的属性: allAddresses for class: java.util.LinkedHashMap$Entry。令我惊讶的是,当键CustomerMaster是数组时,代码可以工作。我不明白如果客户不是数组,它为什么会失败?

代码语言:javascript
运行
复制
import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import groovy.json.JsonSlurper;
import groovy.json.JsonOutput;
import groovy.json.*
def Message removeCustomerNumber(Message message) {
def body = message.getBody(String)
//def body = message.getBody(String.class);
body=body.toString();
def json = new JsonSlurper().parseText(body)
json.CustomerMaster.each{
//    if (it.allAddresses.metaClass.respondsTo('each')){
//    if(it.metaClass.hasProperty(it, 'allAddresses')){
it.allAddresses.each{  
     it.remove('customerNumber')
                    }
 //     }
 //   }     
 }
def out= JsonOutput.toJson(json);
//out = JsonOutput.prettyPrint()
message.setBody(out);
return message;
}

换句话说,当CustomerMaster是一个集合时,我没有问题。我尝试过使用hasProperty()和respondsTo()进行检查,但没有成功。

有效载荷:

代码语言:javascript
运行
复制
{
    "CustomerMaster": {
        "activitySector": "01",
        "cognosCode": "ES001",
        "companyName1": "SANIPLAST SOL. SOSTEN. DEL AGUA SL",
        "companyName2": "ALIAXIS IBERIA S.A.U.",
        "creditControlArea": "ES01",
        "currency": "EUR",
        "customerNumber": "0000100001",
        "allPersons": [
            {
                "birthday": "00000000",
                "cognosCode": "ES001",
                "contactId": "0000000308",
                "customerNumber": "0000100001",
                "jobTitle": "ENCARGADO COBROS",
                "language": "ES",
                "lastName": "EDUARDO JIMENEZ"
            },
            {
                "birthday": "20210419",
                "cognosCode": "ES001",
                "contactId": "0000000475",
                "customerNumber": "0000100001",
                "email1": "FACTURASPDF.SNPT@ABAST.ES",
                "jobTitle": "ENVÍO FACTURACIÓN",
                "language": "ES",
                "lastName": "FACTURACION"
            },
            {
                "birthday": "00000000",
                "cognosCode": "ES001",
                "contactId": "0000000036",
                "customerNumber": "0000100001",
                "jobTitle": "REPRESENTANTE LEGAL",
                "language": "ES",
                "lastName": "D. JULIO ESTEBAN SALGADO"
            }
        ],
        "allAddresses": [
            {
                "StateOrProvince": "MERES-SIERO",
                "city": "MERES-SIERO",
                "countryCode": "ES",
                "customerNumber": "0000100001",
                "fax": "985793802",
                "id": "0000372156",
                "name": "SANIPLAST SOL. SOSTEN. DEL AGUA SL",
                "number": "1",
                "phoneBusiness": "985-792224",
                "postalCode": "33199",
                "region": "33",
                "street": "POL.IND.PRONI. CALLE D",
                "type": "WE"
            },
            {
                "StateOrProvince": "MERES-SIERO",
                "city": "MERES-SIERO",
                "countryCode": "ES",
                "customerNumber": "0000100001",
                "fax": "985793802",
                "id": "0000372156",
                "name": "SANIPLAST SOL. SOSTEN. DEL AGUA SL",
                "number": "1",
                "phoneBusiness": "985-792224",
                "postalCode": "33199",
                "region": "33",
                "street": "POL.IND.PRONI. CALLE D",
                "type": "AG"
            },
            {
                "StateOrProvince": "MERES-SIERO",
                "city": "MERES-SIERO",
                "countryCode": "ES",
                "customerNumber": "0000100001",
                "fax": "985793802",
                "id": "0000372156",
                "name": "SANIPLAST SOL. SOSTEN. DEL AGUA SL",
                "number": "1",
                "phoneBusiness": "985-792224",
                "postalCode": "33199",
                "region": "33",
                "street": "POL.IND.PRONI. CALLE D",
                "type": "RE"
            },
            {
                "StateOrProvince": "MERES-SIERO",
                "city": "MERES-SIERO",
                "countryCode": "ES",
                "customerNumber": "0000100001",
                "fax": "985793802",
                "id": "0000372156",
                "name": "SANIPLAST SOL. SOSTEN. DEL AGUA SL",
                "number": "1",
                "phoneBusiness": "985-792224",
                "postalCode": "33199",
                "region": "33",
                "street": "POL.IND.PRONI. CALLE D",
                "type": "RG"
            },
            {
                "StateOrProvince": "MERES-SIERO",
                "city": "MERES-SIERO",
                "countryCode": "ES",
                "customerNumber": "0000100001",
                "fax": "985793802",
                "id": "0000372156",
                "name": "SANIPLAST SOL. SOSTEN. DEL AGUA SL",
                "number": "1",
                "phoneBusiness": "985-792224",
                "postalCode": "33199",
                "region": "33",
                "street": "POL.IND.PRONI. CALLE D",
                "type": "ZV"
            }
        ],
        "email1": "JUANJOSE.GONZALEZ@SANIPLAST.ES",
        "entityCode": "ES01",
        "fax": "985793802",
        "phone1": "985-792224",
        "phone2": "985791154ISABEL",
        "termsofPayment": "Z037"
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-10-25 07:52:19

代码语言:javascript
运行
复制
def json = new JsonSlurper().parseText(body)
if(json.CustomerMaster instanceof List){
    json.CustomerMaster.each{
        it.allPersons?.each{it.remove("customerNumber")}
        it.allAddresses?.each{it.remove("customerNumber")}
    }
}else{
    json.CustomerMaster?.allPersons?.each{it.remove("customerNumber")}
    json.CustomerMaster?.allAddresses?.each{it.remove("customerNumber")}
}

三值算子

代码语言:javascript
运行
复制
( json.CustomerMaster instanceof List ? json.CustomerMaster : [json.CustomerMaster] ).each{
    it?.allPersons?.each{it.remove("customerNumber")}
    it?.allAddresses?.each{it.remove("customerNumber")}
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74189608

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档