英文:
How do I replace a double quote in json string and regular expressions?
问题
如何在 JSON 字符串和正则表达式中替换双引号?
输入的 JSON 如下:
"RegDateTime" : 1481641851263, "Code":"123213",....
输出应该是:
"RegDateTime" : "1481641851263", "Code":"123213",....
我只想修复 JSON 键值对中的 RegDateTime。请在 Go 语言中提供任何正则表达式,并用双引号进行替换。
英文:
How do I replace a double quote in json string and regular expressions?
Input Json is:
"RegDateTime" : 1481641851263, "Code":"123213",....
and Output should be:
"RegDateTime" : "1481641851263", "Code":"123213",....
I want to fix only json key value that is RegDateTime.
Please suggest any regular expression and replace with double quote in go language.
答案1
得分: 1
func ReplaceAllNumber(json string) (string) {
re := regexp.MustCompile("(:\\s*)(\\d+)(\\s*[,}\\]])")
return re.ReplaceAllString(json, "$1\"$2\"$3")
}
func ReplaceNumberWithField(json string, fieldName string) (string) {
regString := fmt.Sprintf("(\"%s\"\\s*:\\s*)(\\d+)(\\s*[,}\\]])", fieldName)
re := regexp.MustCompile(regString)
return re.ReplaceAllString(json, "$1\"$2\"$3")
}
<details>
<summary>英文:</summary>
func ReplaceAllNumber(json string)(string) {
re := regexp.MustCompile("(:\\s*)(\\d+)(\\s*[,}\\]])")
return re.ReplaceAllString(json, "$1\"$2\"$3")
}
func ReplaceNumberWithField(json string, fieldName string)(string) {
regString := fmt.Sprintf("(\"%s\"\\s*:\\s*)(\\d+)(\\s*[,}\\]])", fieldName)
re := regexp.MustCompile(regString)
return re.ReplaceAllString(json, "$1\"$2\"$3")
}
[Run Online][1]
[1]: https://play.golang.org/p/IzlfVWW1FQ
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论