"sfds\u4e2d双方都\u56fd"
类似这样的,unicode码和中文英文混合,怎么把unicode转换成相应的文字?
{"a":"\u003cp\u003e中国\u003c/p\u003e","b":"hello"}
#2
更多评论
<a href="/user/mulingya" title="@mulingya">@mulingya</a>
```go
package main
import (
"strings"
"bytes"
"fmt"
"strconv"
)
func main() {
str := `sfds\u4e2d双方都\u56fd {"a":"\u003cp\u003e中国\u003c/p\u003e","b":"hello"}`
buf := bytes.NewBuffer(nil)
i, j := 0, len(str)
for i < j {
x := i + 6
if x > j {
buf.WriteString(str[i:])
break
}
if str[i] == '\\' && str[i+1] == 'u' {
hex := str[i+2 : x]
fmt.Println(hex)
r, err := strconv.ParseUint(hex, 16, 64)
if err == nil {
fmt.Println(string(r))
buf.WriteRune(rune(r))
} else {
buf.WriteString(str[i:x])
}
i = x
} else {
buf.WriteByte(str[i])
i++
}
}
//sfds中双方都国 {"a":"<p>中国</p>","b":"hello"}
fmt.Println(buf.String())
}
```
#3