我有一个从JSON-Object转换的字符串,它在每个变量前面有多个转义字符。 我找到了一个删除一组逃逸字符的解决方案。如何清理和格式化我的json字符串?
以下是我的字符串的样子:
{ “FORMFACTOR”: “{\” form_factor_type \ “:\” SOMETHING \ “\ ”payment_card \“:\ ”{\\“ SOMETHING \\ ”:\\“ NOTPRESENT \\”,\\” card_holder_present \\ “:\\” NOTPRESENT \\ “\\ ”UUID \\“:\\ ”SOMETHING \\“,\\ ”INSTRUMENT_ID \\“:\\ ”SOMETHING \\“,\\” signature_id \ \ “:\\” SOMETHING \\ “} \”} “ ”txnCharacteristicsAmt“: ”[\“{\\ ”量\\“:\\ ”{\\\\“ 量\\\\”:\ \\\ “00 \\\\”,\\\\ “代码\\\\”:\\\\ “USD \\\\”} \\ “\\ ”类型\\“:\\” 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 \”:\ “未知\”,\ “pos_condition_code \”: \ “未指定\”,\ “cardholder_authentication_capability \”:\ “未知\”,\ “pos_entry_mode \”:\ “FULL_MAGNETIC_STRIPE \”,\ “card_data_entry_capability_used \”:\ “未知\” } “ ”is_dyson_present“: ”东西“, ”productInfo“: ”{\“ 租客\ ”:\“ SOMETHING \”,\ “一体化\”:\ “SOMETHING \”,\ “PRODUCT_NAME \”:\“SOMETHING \ “\ ”tokenized_value_type \“:\ ”SOMETHING \“}”, “cashOverAmount”: “东西”, “counterPrtyAccNum”: “东西”, “merchantDetails”: “{\” merchant_address \ “:\”{\\ “状态\\”:\\ “SOMETHING \\”,\\ “地址1 \\”:\\ “SOMETHING \\”,\\ “城市\\”:\\ “SOMETHING \\”,\\“iso_country \\ “:\\” 美国\\ “} \”,\ “merchant_category_code \”:\ “SOMETHING \”,\ “MERCHANT_NAME \”:\ “SOMETHING \”,\ “issuer_list \”:\“[\\ “SOMETHING \\”] \”,\ “external_merchant_id \”:\ “SOMETHING \”,\ “acquiring_institution_country_code \”:\ “US \”}}
答案 0 :(得分:4)
生成该字符串的人确实搞砸了。严重!!
每个嵌套对象都独立转换为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\"
}
}