我很难从子数组customerNumber和allPersons中删除键‘allAddresses’。我的要求是,在下面的有效负载键中,customerNumber应该只出现在父数组CustomerMaster中。customerNumber的所有其他外观都应删除。
对于下面的代码,我得到了错误groovy.lang.MissingPropertyException: No这样的属性: allAddresses for class: java.util.LinkedHashMap$Entry。令我惊讶的是,当键CustomerMaster是数组时,代码可以工作。我不明白如果客户不是数组,它为什么会失败?
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()进行检查,但没有成功。
有效载荷:
{
"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"
}
}
发布于 2022-10-25 07:52:19
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")}
}
三值算子
( json.CustomerMaster instanceof List ? json.CustomerMaster : [json.CustomerMaster] ).each{
it?.allPersons?.each{it.remove("customerNumber")}
it?.allAddresses?.each{it.remove("customerNumber")}
}
https://stackoverflow.com/questions/74189608
复制相似问题