gpt4 book ai didi

java - 如何清除 json 字符串中的多个转义字符?

转载 作者:行者123 更新时间:2023-12-01 17:59:31 25 4
gpt4 key购买 nike

我有一个从 JSON 对象转换而来的字符串,该字符串在每个变量之前都有多个转义字符。我找到了删除一组转义字符的解决方案。如何清理和格式化我的 json 字符串?

这是我的字符串的样子:

{"formFactor":"{\"form_factor_type\":\"SOMETHING\",\"payment_card\":\"{\\"SOMETHING\\":\\"NOTPRESENT\\",\\"card_holder_present\\":\\"NOTPRESENT\\",\\"UUID\\":\\"SOMETHING\\",\\"instrument_id\\":\\"SOMETHING\\",\\"signature_id\\":\\"SOMETHING\\"}\"}","txnCharacteristicsAmt":"[\"{\\"amount\\":\\"{\\\\"amount\\\\":\\\\"00\\\\",\\\\"code\\\\":\\\\"USD\\\\"}\\",\\"type\\":\\"CASH_OVER\\"}\"]","pmtCardInfo":"{\"card_present\":\"PRESENT\",\"card_holder_present\":\"PRESENT\",\"UUID\":\"SOMETHING\",\"instrument_id\":\"SOMETHING\"}","terminalInfo":"{\"cardholder_authentication_capability_used\":\"SIGNATURE\",\"pin_entry_capability\":\"UNKNOWN\",\"operating_environment\":\"ADMINISTRATIVE_TERMINAL\",\"terminal_id\":\"SOMETHING\",\"pan_entry_mode\":\"FULL_MAGNETIC_STRIPE\",\"card_data_entry_capability\":\"UNKNOWN\",\"pos_condition_code\":\"UNSPECIFIED\",\"cardholder_authentication_capability\":\"UNKNOWN\",\"pos_entry_mode\":\"FULL_MAGNETIC_STRIPE\",\"card_data_entry_capability_used\":\"UNKNOWN\"}","is_dyson_present":"SOMETHING","productInfo":"{\"tenant\":\"SOMETHING\",\"integration\":\"SOMETHING\",\"product_name\":\"SOMETHING\",\"tokenized_value_type\":\"SOMETHING\"}","cashOverAmount":"SOMETHING","counterPrtyAccNum":"SOMETHING","merchantDetails":"{\"merchant_address\":\"{\\"state\\":\\"SOMETHING\\",\\"address1\\":\\"SOMETHING\\",\\"city\\":\\"SOMETHING\\",\\"iso_country\\":\\"US\\"}\",\"merchant_category_code\":\"SOMETHING\",\"merchant_name\":\"SOMETHING\",\"issuer_list\":\"[\\"SOMETHING\\"]\",\"external_merchant_id\":\"SOMETHING\",\"acquiring_institution_country_code\":\"US\"}}

最佳答案

无论是谁生成了这个字符串,都搞砸了。糟糕!!

每个嵌套对象都独立转换为 JSON 文本,然后将该文本分配为字符串值,而不是将整个对象层次结构转换为单个 JSON 结构。

然后,他们错误地对嵌套字符串进行编码,导致生成错误的 JSON。显然他们没有使用好的 JSON 库。

正确的解决方案是修复生成该字符串的代码。

下面是为人类可读而格式化的字符串。问题在于根对象以 formFactor 字段开头。它的值是一个对象,但该对象不是将对象作为字段值,而是将其转换为 JSON 文本,并且 formFactor 字段将文本作为其值。对第二个字段 ( payment_card ) 重复此操作,但转义错误:

"formFactor":"{
\"form_factor_type\":\"SOMETHING\",
\"payment_card\":\"{
\\"SOMETHING\\":\\"NOTPRESENT\\",
\\"card_holder_present\\":\\"NOTPRESENT\\",
\\"UUID\\":\\"SOMETHING\\",
\\"instrument_id\\":\\"SOMETHING\\",
\\"signature_id\\":\\"SOMETHING\\"
}\"
}"

双重嵌套对象的 JSON 文本编码不正确。要了解这一点,让我们从内部开始。 payment_card 对象是:

{
"SOMETHING": "NOTPRESENT",
"card_holder_present": "NOTPRESENT",
"UUID": "SOMETHING",
"instrument_id": "SOMETHING",
"signature_id": "SOMETHING"
}

当该文本被编码为 formFactor 对象的 JSON 字符串时,所有引号 (") 都会被转义:

{
"form_factor_type": "SOMETHING",
"payment_card": "{
\"SOMETHING\": \"NOTPRESENT\",
\"card_holder_present\": \"NOTPRESENT\",
\"UUID\": \"SOMETHING\",
\"instrument_id\": \"SOMETHING\",
\"signature_id\": \"SOMETHING\"
}"
}

当该文本再次编码为根对象的 formFactor 字段的 JSON 字符串时,所有反斜杠 (\) 和引号 (") 被转义:

"formFactor": "{
\"form_factor_type\": \"SOMETHING\",
\"payment_card\": \"{
\\\"SOMETHING\\\": \\\"NOTPRESENT\\\",
\\\"card_holder_present\\\": \\\"NOTPRESENT\\\",
\\\"UUID\\\": \\\"SOMETHING\\\",
\\\"instrument_id\\\": \\\"SOMETHING\\\",
\\\"signature_id\\\": \\\"SOMETHING\\\"
}\"
}"

但正如您所看到的,事实并非如此。您的字符串中的反斜杠 (\) 数量错误,即您的字符串已损坏

修复生成字符串的代码。通过正确转义多重嵌套字符串,不独立地将嵌套对象转换为JSON 文本。我建议第二种选择,即上面的内容应该是:

"formFactor": {
"form_factor_type": "SOMETHING",
"payment_card": {
"SOMETHING": "NOTPRESENT",
"card_holder_present": "NOTPRESENT",
"UUID": "SOMETHING",
"instrument_id": "SOMETHING",
"signature_id": "SOMETHING"
}
}

完整格式化字符串

{
"formFactor":"{
\"form_factor_type\":\"SOMETHING\",
\"payment_card\":\"{
\\"SOMETHING\\":\\"NOTPRESENT\\",
\\"card_holder_present\\":\\"NOTPRESENT\\",
\\"UUID\\":\\"SOMETHING\\",
\\"instrument_id\\":\\"SOMETHING\\",
\\"signature_id\\":\\"SOMETHING\\"
}\"
}",
"txnCharacteristicsAmt":"[
\"{
\\"amount\\":\\"{
\\\\"amount\\\\":\\\\"00\\\\",
\\\\"code\\\\":\\\\"USD\\\\"
}\\",
\\"type\\":\\"CASH_OVER\\"
}\"
]",
"pmtCardInfo":"{
\"card_present\":\"PRESENT\",
\"card_holder_present\":\"PRESENT\",
\"UUID\":\"SOMETHING\",
\"instrument_id\":\"SOMETHING\"
}",
"terminalInfo":"{
\"cardholder_authentication_capability_used\":\"SIGNATURE\",
\"pin_entry_capability\":\"UNKNOWN\",
\"operating_environment\":\"ADMINISTRATIVE_TERMINAL\",
\"terminal_id\":\"SOMETHING\",
\"pan_entry_mode\":\"FULL_MAGNETIC_STRIPE\",
\"card_data_entry_capability\":\"UNKNOWN\",
\"pos_condition_code\":\"UNSPECIFIED\",
\"cardholder_authentication_capability\":\"UNKNOWN\",
\"pos_entry_mode\":\"FULL_MAGNETIC_STRIPE\",
\"card_data_entry_capability_used\":\"UNKNOWN\"
}",
"is_dyson_present":"SOMETHING",
"productInfo":"{
\"tenant\":\"SOMETHING\",
\"integration\":\"SOMETHING\",
\"product_name\":\"SOMETHING\",
\"tokenized_value_type\":\"SOMETHING\"
}",
"cashOverAmount":"SOMETHING",
"counterPrtyAccNum":"SOMETHING",
"merchantDetails":"{
\"merchant_address\":\"{
\\"state\\":\\"SOMETHING\\",
\\"address1\\":\\"SOMETHING\\",
\\"city\\":\\"SOMETHING\\",
\\"iso_country\\":\\"US\\"
}\",
\"merchant_category_code\":\"SOMETHING\",
\"merchant_name\":\"SOMETHING\",
\"issuer_list\":\"[
\\"SOMETHING\\"
]\",
\"external_merchant_id\":\"SOMETHING\",
\"acquiring_institution_country_code\":\"US\"
}
}

关于java - 如何清除 json 字符串中的多个转义字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42144984/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com