2016-12-16 137 views
-2

如何替換json字符串和正則表達式中的雙引號?如何替換json字符串和正則表達式中的雙引號?

輸入JSON是:

"RegDateTime" : 1481641851263, "Code":"123213",.... 

和輸出應該是:

"RegDateTime" : "1481641851263", "Code":"123213",.... 

我想修復只JSON鍵值是RegDateTime。
請建議任何正則表達式,並用go語言中的雙引號替換。

+0

這是錯誤的方式去思考它。這是一個_數字。您將數字轉換爲字符串......不僅僅是「替換引號」。 –

+0

只是轉換爲字符串。 –

+0

@JeffMercado其他的方法。他希望將數字輸入轉換爲字符串表示形式。 –

回答

0
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

+0

謝謝chaoluo ...這種方法怎麼樣? ? \t re,_:= regexp.Compile(「(RegDateTime \」):([0-9] +)「)result:= re.ReplaceAllString(json,」$ 1:\「$ 2 \」「) –

+0

您需要只需要替換一個特殊字段? – chaoluo

+0

...我的方法是替換所有號碼字段,所以你可以換成特殊字段,那很簡單 – chaoluo

相關問題