英文:
How to convert unicode byte array to normal string in go
问题
我正在从Unix套接字获取字节数组,并尝试将其作为字符串打印出来。我只是使用string(bytes)
并得到以下字符串。
{"Created":1410263175,"Id":"f4e36130333537c3725e212f78d603742cf3da4b738272f7232338b0d61fa4fb","ParentId":"a8a806a76e3e620a6f2172e401847beb4535b072cf7e60d31e91becc3986827e","RepoTags":["\u003cnone\u003e:\u003cnone\u003e"],"Size":0,"VirtualSize":1260903901},
如何去除转义字符\
并将Unicode字符\u003
转换为普通字符串?
英文:
I'm getting the byte array from unix socket and try to print as a string. I just string(bytes)
and get the following string.
<pre>
{"Created":1410263175,"Id":"f4e36130333537c3725e212f78d603742cf3da4b738272f7232338b0d61fa4fb","ParentId":"a8a806a76e3e620a6f2172e401847beb4535b072cf7e60d31e91becc3986827e","RepoTags":["\u003cnone\u003e:\u003cnone\u003e"],"Size":0,"VirtualSize":1260903901}\n,
</pre>
How can I remove the escape char \
and convert unicode char \u003
into normal string?
答案1
得分: 2
这看起来像是一个符合JSON规范的带有\u转义的JSON字符串。JSON解码器会负责取消转义字符串。
代码如下:
s := "{\"Created\":1410263175,\"Id\":\"f4e36130333537c3725e212f78d603742cf3da4b738272f7232338b0d61fa4fb\",\"ParentId\":\"a8a806a76e3e620a6f2172e401847beb4535b072cf7e60d31e91becc3986827e\",\"RepoTags\":[\"\\u003cnone\\u003e:\\u003cnone\\u003e\"],\"Size\":0,\"VirtualSize\":1260903901}\n"
var m map[string]interface{}
if err := json.Unmarshal([]byte(s), &m); err != nil {
log.Fatal(err)
}
fmt.Printf("%#v", m)
打印结果如下(去除了我为了可读性而添加的空格):
map[string]interface{}{
"Created": 1.410263175e+09,
"Id": "f4e36130333537c3725e212f78d603742cf3da4b738272f7232338b0d61fa4fb",
"ParentId": "a8a806a76e3e620a6f2172e401847beb4535b072cf7e60d31e91becc3986827e",
"RepoTags": []interface{}{"<none>:<none>"},
"Size": 0,
"VirtualSize": 1.260903901e+09,
}
[kbd][playground](http://play.golang.org/p/3MNwnQC6_-)[/kbd]
在Go中,将字节转换为字符串时不会创建\u转义。它是由JSON编码器生成的字节序列的一部分。字符串转换运算符string(byteSlice)会将这些字节原样转换为字符串。
<details>
<summary>英文:</summary>
This looks like a JSON string with \u escapes per the JSON specification. The JSON decoder will take care of unescaping the strings.
The code:
s := "{\"Created\":1410263175,\"Id\":\"f4e36130333537c3725e212f78d603742cf3da4b738272f7232338b0d61fa4fb\",\"ParentId\":\"a8a806a76e3e620a6f2172e401847beb4535b072cf7e60d31e91becc3986827e\",\"RepoTags\":[\"\\u003cnone\\u003e:\\u003cnone\\u003e\"],\"Size\":0,\"VirtualSize\":1260903901}\n"
var m map[string]interface{}
if err := json.Unmarshal([]byte(s), &m); err != nil {
log.Fatal(err)
}
fmt.Printf("%#v", m)
prints the following (minus the white space that I added for readability):
map[string]interface {}{
"Created":1.410263175e+09,
"Id":"f4e36130333537c3725e212f78d603742cf3da4b738272f7232338b0d61fa4fb",
"ParentId":"a8a806a76e3e620a6f2172e401847beb4535b072cf7e60d31e91becc3986827e",
"RepoTags":[]interface {}{"<none>:<none>"},
"Size":0,
"VirtualSize":1.260903901e+09}
<kbd>[playground](http://play.golang.org/p/3MNwnQC6_-)</kbd>
The \u escape is not created when converting bytes to a string in Go. It's part of the byte sequence generated by a JSON encoder. The string conversion operator string(byteSlice) converts these bytes to a string as is.
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论