Java 我需要从JSON对象中删除一个特定的标记

Java 我需要从JSON对象中删除一个特定的标记,java,json,Java,Json,我有一个JSON对象,如下所示 { "mandator":"GB0010001", "debitAccount":"81884", "creditAccount":"82918", "trustedBeneficiary":"false", "localCurrencyAmount":35, "transactionReference":"omega7.1.1", "debitAccountASPSP":"t24", "curren

我有一个JSON对象,如下所示

{
    "mandator":"GB0010001",
    "debitAccount":"81884",
    "creditAccount":"82918",
    "trustedBeneficiary":"false",
    "localCurrencyAmount":35,
    "transactionReference":"omega7.1.1",
    "debitAccountASPSP":"t24",
    "currencyAmount":35,
    "executionDate":"20180102",
    "creditAccountASPSP":"t24",
    "transactionType":"Contactless payment",
    "trustedPSP":"false",
    "jsonErrorResponse":{
        "errorCount":0,
        "errors":[

        ]
    },
    "currency":"USD",
    "company":"GB0010001"
}
我需要删除
“jsonErrorResponse”:{“errorCount”:0,“errors”:[]}
, 我使用了
JSONobject.remove(“jsonErrorResponse”).toString()

但是它给我的输出是
{“errorCount”:0,“errors:[]}
而不是

{
    "mandator":"GB0010001",
    "debitAccount":"81884",
    "creditAccount":"82918",
    "trustedBeneficiary":"false",
    "localCurrencyAmount":35,
    "transactionReference":"omega7.1.1",
    "debitAccountASPSP":"t24",
    "currencyAmount":35,
    "executionDate":"20180102",
    "creditAccountASPSP":"t24",
    "transactionType":"Contactless payment",
    "trustedPSP":"false",
    "currency":"USD",
    "company":"GB0010001"
}
JSONobject.remove(“jsonErrorResponse”)
返回已删除的内容。您正在对刚刚“删除”的部分调用
toString

只是不要链接方法调用

JSONobject.remove("jsonErrorResponse");
String newJson = JSONobject.toString()
JSONobject.remove(“jsonErrorResponse”)
返回已删除的内容。您正在对刚刚“删除”的部分调用
toString

只是不要链接方法调用

JSONobject.remove("jsonErrorResponse");
String newJson = JSONobject.toString()

remove方法很可能返回已删除的对象。
您需要对从中删除的对象执行字符串操作。删除方法很可能返回已删除的对象。
由于您正在处理
JSONobject
您正在从该对象中删除内容,因此您需要对从中删除的对象执行字符串操作

您需要在
JSONobject
上调用
toString()

String str = "{\"mandator\":\"GB0010001\",\"debitAccount\":\"81884\",\"creditAccount\":\"82918\",\"trustedBeneficiary\":\"false\",\"localCurrencyAmount\":35,\"transactionReference\":\"omega7.1.1\",\"debitAccountASPSP\":\"t24\",\"currencyAmount\":35,\"executionDate\":\"20180102\",\"creditAccountASPSP\":\"t24\",\"transactionType\":\"Contactless payment\",\"trustedPSP\":\"false\",\"jsonErrorResponse\":{\"errorCount\":0,\"errors\":[]},\"currency\":\"USD\",\"company\":\"GB0010001\"}"
JSONObject jsonObject = new JSONObject(str);
jsonObject.remove("jsonErrorResponse");
jsonObject.toString();

由于您正在处理
JSONobject
,因此正在从该对象中删除内容

您需要在
JSONobject
上调用
toString()

String str = "{\"mandator\":\"GB0010001\",\"debitAccount\":\"81884\",\"creditAccount\":\"82918\",\"trustedBeneficiary\":\"false\",\"localCurrencyAmount\":35,\"transactionReference\":\"omega7.1.1\",\"debitAccountASPSP\":\"t24\",\"currencyAmount\":35,\"executionDate\":\"20180102\",\"creditAccountASPSP\":\"t24\",\"transactionType\":\"Contactless payment\",\"trustedPSP\":\"false\",\"jsonErrorResponse\":{\"errorCount\":0,\"errors\":[]},\"currency\":\"USD\",\"company\":\"GB0010001\"}"
JSONObject jsonObject = new JSONObject(str);
jsonObject.remove("jsonErrorResponse");
jsonObject.toString();

查看
remove
方法的javadoc。然后在
删除
“jsonErrorResponse”
后查看
JSONObject
。查看
remove
方法的javadoc。然后在
删除
后查看
JSONObject
“jsonErrorResponse”