duandange7480 2016-10-21 11:34
浏览 436
已采纳

转:反序列化数组字符串

I have a string as: - ["a","b","c"]. How to parse / convert it into a Go array? I can do string parsing but is there any out of the box function in Go for the same.

  • 写回答

1条回答 默认 最新

  • dongshangan2074 2016-10-21 11:41
    关注

    How about using json.Unmarshal()?

    s := `["a","b","c"]`
    
    var arr []string
    if err := json.Unmarshal([]byte(s), &arr); err != nil {
        fmt.Println("Error:", err)
    }
    fmt.Printf("%q", arr)
    

    Output (try it on the Go Playground):

    ["a" "b" "c"]
    

    But know that package json does a lot of reflection kung-fu under the hood, it's faster if you write the parsing yourself. On the other hand, package json will also handle random white-spaces in the input – even newline characters and Unicode sequences, like this one (it's equivalent to ["a","b","c"]):

    s := `[ "a" , "b"  
     ,"\u0063"  ]  `
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗