【发布时间】:2020-04-14 19:38:12
【问题描述】:
我有一个 JSON 字符串如下:
{
"account": "1234",
"type": "ar-type",
"eventTypes": "Update",
"objectClassName": "com.triype",
"objectJson": "{\"Name\":\"pdpot\",\"traptype\":\"adpot",\"displayName\":\"pdpot",\"experimentName\":\"pdpotpie\",\"creationTime\":\"Mar 18, 2020 5:58:58 PM\",\"createdBy\":{\"userProfileOne\":\"s:pdx\",\"userProfileTwo\":\"sid\",\"domainId\":\"did:pdx-tod-64003\"},\"lastModifiedBy\":{\"userProfileArn\":\"s:pdx-tod-64003\"},\"createdBy\":{\"userProfileOne\":\"s:p\",\"userProfileTwo\":\"si\",\"domainId\":\"did:ppot\"}}}
}
我将此输入作为字符串获取,在将其作为字符串传递给解析器之前,我需要执行一些字符串过滤并删除所有“userProfileOne”、“userProfileTwo”、“domainId”及其键,而不会影响 JSON 结构.我目前正在使用 gson 和 json 在 Java 中编写此代码。
注意:UserProfileOne、UserProfileTwo 和 DomainID 有多次出现。
所需的输出如下:
{
"account": "1234",
"type": "ar-type",
"eventTypes": "Update",
"objectClassName": "com.triype",
"objectJson": "{\"Name\":\"pdpot\",\"traptype\":\"adpot",\"displayName\":\"pdpot",\"experimentName\":\"pdpotpie\",\"creationTime\":\"Mar 18, 2020 5:58:58 PM\"}}
}
目前我正在使用 substringBetween。但是操作并没有按预期进行。
【问题讨论】:
-
为什么“objectJson”的内容中有转义字符\?
-
请显示您目前使用的代码,以及您遇到的确切问题。
-
可能是因为它是一个字符串
-
看起来整个 JSON 格式不正确,例如
adpot之后的引号没有转义。在这种情况下你可以使用正则表达式来切割不必要的片段\\"(userProfileOne|userProfileTwo|domainId)\\"\s*:\s*\\"(.+?)\\"\s*,?