How to convert unicode byte array to normal string in go

huangapple go评论126阅读模式
英文:

How to convert unicode byte array to normal string in go

问题

我正在从Unix套接字获取字节数组,并尝试将其作为字符串打印出来。我只是使用string(bytes)并得到以下字符串。

  1. {"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>
{&quot;Created&quot;:1410263175,&quot;Id&quot;:&quot;f4e36130333537c3725e212f78d603742cf3da4b738272f7232338b0d61fa4fb&quot;,&quot;ParentId&quot;:&quot;a8a806a76e3e620a6f2172e401847beb4535b072cf7e60d31e91becc3986827e&quot;,&quot;RepoTags&quot;:[&quot;\u003cnone\u003e:\u003cnone\u003e&quot;],&quot;Size&quot;:0,&quot;VirtualSize&quot;:1260903901}\n,
</pre>

How can I remove the escape char \ and convert unicode char \u003 into normal string?

答案1

得分: 2

这看起来像是一个符合JSON规范的带有\u转义的JSON字符串。JSON解码器会负责取消转义字符串。

代码如下:

  1. s := "{\"Created\":1410263175,\"Id\":\"f4e36130333537c3725e212f78d603742cf3da4b738272f7232338b0d61fa4fb\",\"ParentId\":\"a8a806a76e3e620a6f2172e401847beb4535b072cf7e60d31e91becc3986827e\",\"RepoTags\":[\"\\u003cnone\\u003e:\\u003cnone\\u003e\"],\"Size\":0,\"VirtualSize\":1260903901}\n"
  2. var m map[string]interface{}
  3. if err := json.Unmarshal([]byte(s), &m); err != nil {
  4. log.Fatal(err)
  5. }
  6. fmt.Printf("%#v", m)

打印结果如下(去除了我为了可读性而添加的空格):

  1. map[string]interface{}{
  2. "Created": 1.410263175e+09,
  3. "Id": "f4e36130333537c3725e212f78d603742cf3da4b738272f7232338b0d61fa4fb",
  4. "ParentId": "a8a806a76e3e620a6f2172e401847beb4535b072cf7e60d31e91becc3986827e",
  5. "RepoTags": []interface{}{"<none>:<none>"},
  6. "Size": 0,
  7. "VirtualSize": 1.260903901e+09,
  8. }
  9. [kbd][playground](http://play.golang.org/p/3MNwnQC6_-)[/kbd]
  10. Go将字节转换为字符串时不会创建\u转义它是由JSON编码器生成的字节序列的一部分字符串转换运算符string(byteSlice)会将这些字节原样转换为字符串
  11. <details>
  12. <summary>英文:</summary>
  13. This looks like a JSON string with \u escapes per the JSON specification. The JSON decoder will take care of unescaping the strings.
  14. The code:
  15. s := &quot;{\&quot;Created\&quot;:1410263175,\&quot;Id\&quot;:\&quot;f4e36130333537c3725e212f78d603742cf3da4b738272f7232338b0d61fa4fb\&quot;,\&quot;ParentId\&quot;:\&quot;a8a806a76e3e620a6f2172e401847beb4535b072cf7e60d31e91becc3986827e\&quot;,\&quot;RepoTags\&quot;:[\&quot;\\u003cnone\\u003e:\\u003cnone\\u003e\&quot;],\&quot;Size\&quot;:0,\&quot;VirtualSize\&quot;:1260903901}\n&quot;
  16. var m map[string]interface{}
  17. if err := json.Unmarshal([]byte(s), &amp;m); err != nil {
  18. log.Fatal(err)
  19. }
  20. fmt.Printf(&quot;%#v&quot;, m)
  21. prints the following (minus the white space that I added for readability):
  22. map[string]interface {}{
  23. &quot;Created&quot;:1.410263175e+09,
  24. &quot;Id&quot;:&quot;f4e36130333537c3725e212f78d603742cf3da4b738272f7232338b0d61fa4fb&quot;,
  25. &quot;ParentId&quot;:&quot;a8a806a76e3e620a6f2172e401847beb4535b072cf7e60d31e91becc3986827e&quot;,
  26. &quot;RepoTags&quot;:[]interface {}{&quot;&lt;none&gt;:&lt;none&gt;&quot;},
  27. &quot;Size&quot;:0,
  28. &quot;VirtualSize&quot;:1.260903901e+09}
  29. &lt;kbd&gt;[playground](http://play.golang.org/p/3MNwnQC6_-)&lt;/kbd&gt;
  30. The \u escape is not created when converting bytes to a string in Go. It&#39;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.
  31. </details>

huangapple
  • 本文由 发表于 2014年10月13日 09:50:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/26331886.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定