How to convert unicode byte array to normal string in go

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

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>
{&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解码器会负责取消转义字符串。

代码如下:

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 := &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;
	var m map[string]interface{}
	if err := json.Unmarshal([]byte(s), &amp;m); err != nil {
		log.Fatal(err)
	}
	fmt.Printf(&quot;%#v&quot;, m)

prints the following (minus the white space that I added for readability):

    map[string]interface {}{
         &quot;Created&quot;:1.410263175e+09, 
         &quot;Id&quot;:&quot;f4e36130333537c3725e212f78d603742cf3da4b738272f7232338b0d61fa4fb&quot;,
         &quot;ParentId&quot;:&quot;a8a806a76e3e620a6f2172e401847beb4535b072cf7e60d31e91becc3986827e&quot;, 
         &quot;RepoTags&quot;:[]interface {}{&quot;&lt;none&gt;:&lt;none&gt;&quot;}, 
         &quot;Size&quot;:0, 
         &quot;VirtualSize&quot;:1.260903901e+09}

&lt;kbd&gt;[playground](http://play.golang.org/p/3MNwnQC6_-)&lt;/kbd&gt;

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.

</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:

确定