如何在JSON响应中删除特定键

时间:2019-06-18 09:54:46

标签: javascript json postman

例如,我收到邮递员的JSON响应并将其存储在变量中。 如何从此响应中删除特定密钥?

在我的示例中,我要删除:head_out_timestam

我需要用JavaScript编写,谢谢。

{
"soapenv:Envelope": {
    "$": {
        "xmlns:soapenv": "http://schemas.xmlsoap.org/soap/envelope/"
    },
    "soapenv:Header": {
        "f:Routing": {
            "f:Receiver": {
                "f:Application": "Something"
            }
        }
    },
    "soapenv:Body": {
        "Something": {
            "something_output": {
                "service_header_out": {
                    "head_out_servercode": "Some",
                    "head_out_timestam": "2019-06-18-11.32.13.570127",
                }
            }
        }
    }
}

}

4 个答案:

答案 0 :(得分:1)

您可以对对象中的键进行递归搜索,然后删除找到的键。

检查以下解决方案:

var json = {
"soapenv:Envelope": {
    "$": {
        "xmlns:soapenv": "http://schemas.xmlsoap.org/soap/envelope/"
    },
    "soapenv:Header": {
        "f:Routing": {
            "f:Receiver": {
                "f:Application": "Something"
            }
        }
    },
    "soapenv:Body": {
        "Something": {
            "something_output": {
                "service_header_out": {
                    "head_out_servercode": "Some",
                    "head_out_timestam": "2019-06-18-11.32.13.570127",
                }
            }
        }
    }
}
}

var removeKey = (json, key) => {
  Object.keys(json).forEach((item) => {
  	if (item !== key) {
      // make sure json[item] is an object
    	if (typeof json[item] === 'object') {
      	removeKey(json[item], key)
      }
    } else {
    	delete json[item];
    }
  })
  return json;
}
console.log(removeKey(json, 'head_out_timestam'))

注意:您可以使用Object.keys(obj).forEachfor ... in

答案 1 :(得分:0)

var response = {
    "soapenv:Envelope": {
        "$": {
            "xmlns:soapenv": "http://schemas.xmlsoap.org/soap/envelope/"
        },
        "soapenv:Header": {
            "f:Routing": {
                "f:Receiver": {
                    "f:Application": "Something"
                }
            }
        },
        "soapenv:Body": {
            "Something": {
                "something_output": {
                    "service_header_out": {
                        "head_out_servercode": "Some",
                        "head_out_timestam": "2019-06-18-11.32.13.570127",
                    }
                }
            }
        }
    }
}

console.log(response["soapenv:Envelope"]["soapenv:Body"]["Something"]["something_output"]["service_header_out"]["head_out_timestam"]);

delete response["soapenv:Envelope"]["soapenv:Body"]["Something"]["something_output"]["service_header_out"]["head_out_timestam"]
console.log(response["soapenv:Envelope"]["soapenv:Body"]["Something"]["something_output"]["service_header_out"]["head_out_timestam"]);

答案 2 :(得分:0)

用于删除 head_out_timestam

只需尝试这种方式

 let response={
    "soapenv:Envelope": {
        "$": {
            "xmlns:soapenv": "http://schemas.xmlsoap.org/soap/envelope/"
        },
        "soapenv:Header": {
            "f:Routing": {
                "f:Receiver": {
                    "f:Application": "Something"
                }
            }
        },
        "soapenv:Body": {
            "Something": {
                "something_output": {
                    "service_header_out": {
                        "head_out_servercode": "Some",
                        "head_out_timestam": "2019-06-18-11.32.13.570127"
                    }
                }
            }
        }
    }
    }
    //for deleting the required key-value 
 

delete response["soapenv:Envelope"]["soapenv:Body"]["Something"]["something_output"]["service_header_out"]["head_out_timestam"]
    
    //Check the new response 
    console.log(response);

答案 3 :(得分:0)

根据您可以简单使用的示例,您可以在javascript中使用delete运算符,

删除响应[“” soapenv:信封“”] [“” soapenv:正文“”]。Something.something_output.service_header_out.head_out_timestam;