json字符串双引号 解析错误

  String tdKeyExtAttributeValues =
                "[{"'attrValue":"65g/3个。品味着"下午嗨到晚上",西班牙式休闲文化。"}
                ,{"attrValue":"大家伙们:"你是大各自"晚上好?"}
                ,{"keyExtAttrName":"minQty","keyExtAttrId":"042658ec-203d-40cb-894c-7c69f7571e29","attrValue":"0"}]";

                // 替换匹配[中文字符后的",同时去除"后的}符合的字符串]
String s = tdKeyExtAttributeValues.replaceAll("(?<=[\\u4E00-\\u9FA5])\\\"(?![},])", "'")
                // 替换匹配前后有中文标点符号的 " 的字符串
                .replaceAll("(?<=[\\u3002\\uff1b\\uff0c\\uff1a\\u201c\\u201d\\uff08\\uff09\\u3001\\uff1f\\u300a\\u300b!])\\\"(?![}])|(?<!:)\\\"(?=[\\u3002\\uff1b\\uff0c\\uff1a\\u201c\\u201d\\uff08\\uff09\\u3001\\uff1f\\u300a\\u300b!])", ":'");
		// 将json字符串转换成对象
        List<Map<String, String>> tdKeyExtAttributeValuesList9 = JSON.parseObject(s, ArrayList.class);

正则表达式 - (?!), (?:\), (?=) 知识转
1。https://www.cnblogs.com/allen2333/p/9835654.html
2。https://www.cnblogs.com/qize/p/12887542.html

正则表达式中文字符
[\u4E00-\u9FA5]
正则表达式中文符号
[\u3002\uff1b\uff0c\uff1a\u201c\u201d\uff08\uff09\u3001\uff1f\u300a\u300b!]
。 ; , : “ ”( ) 、 ? 《 》! 这些标点符号

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在C语言中,可以使用正则表达式解析JSON字符串。以下是一个简单的示例代码,它使用正则表达式解析JSON字符串并提取其中的键值对: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <regex.h> #define MAX_MATCHES 10 // 正则表达式匹配错误处理函数 void handle_regex_error(int error_code, regex_t *regex) { char error_message[100]; regerror(error_code, regex, error_message, sizeof(error_message)); printf("Regex error: %s\n", error_message); exit(EXIT_FAILURE); } int main() { char *json_str = "{\"name\":\"John\", \"age\":30, \"city\":\"New York\"}"; char *pattern = "\"(\\w+)\":\"?([^\"]+)\"?"; regex_t regex; int ret = regcomp(&regex, pattern, REG_EXTENDED); if (ret != 0) { handle_regex_error(ret, &regex); } regmatch_t matches[MAX_MATCHES]; int num_matches = 0; int start_pos = 0; while (1) { ret = regexec(&regex, json_str + start_pos, MAX_MATCHES, matches, 0); if (ret == REG_NOMATCH) { break; } else if (ret != 0) { handle_regex_error(ret, &regex); } num_matches = matches[0].rm_eo - matches[0].rm_so; char match[num_matches + 1]; memset(match, 0, sizeof(match)); strncpy(match, json_str + start_pos + matches[0].rm_so, num_matches); printf("Key: %.*s\n", matches[1].rm_eo - matches[1].rm_so, json_str + start_pos + matches[1].rm_so); printf("Value: %.*s\n", matches[2].rm_eo - matches[2].rm_so, json_str + start_pos + matches[2].rm_so); start_pos += matches[0].rm_eo; } regfree(&regex); return 0; } ``` 在这个示例中,我们使用了一个正则表达式来匹配JSON字符串中的键值对。正则表达式的模式为:`"(\w+)":"?([^"]+)"?`,其中: - `\"` 表示双引号,`\w` 表示任意字母、数字或下划线; - `(\w+)` 表示匹配一个或多个字母、数字或下划线,并将其作为第一个子匹配; - `:` 表示冒号; - `"?` 表示可选的双引号; - `([^"]+)` 表示匹配一个或多个非双引号字符,并将其作为第二个子匹配; - `"?"` 表示可选的双引号。 在代码中,我们使用了 `regcomp` 函数来编译正则表达式模式,然后使用 `regexec` 函数来匹配字符串。每次匹配成功后,我们使用 `regmatch_t` 结构体来存储匹配结果,并使用 `strncpy` 函数将匹配的结果复制到一个新的字符串中。最后,我们在循环中更新起始位置,直到整个字符串被处理完毕。 需要注意的是,这只是一个简单的示例代码,仅适用于特定的JSON字符串格式。对于更复杂的JSON字符串,可能需要更复杂的正则表达式模式来解析。此外,使用正则表达式解析JSON字符串可能不如使用专门的JSON解析库来得方便和安全。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值