duannaxin9975
2014-07-11 03:18
浏览 497

转到:将字符串数组转换为Json数组字符串

Trying to convert a strings array to a json string in Go. But all I get is an array of numbers.

What am I missing?

package main

import (
    "fmt"
    "encoding/json"
)

func main() {
    var urls = []string{
        "http://google.com",
        "http://facebook.com",
        "http://youtube.com",
        "http://yahoo.com",
        "http://twitter.com",
        "http://live.com",
    }

    urlsJson, _ := json.Marshal(urls)
    fmt.Println(urlsJson)
}

Code on Go Playground: http://play.golang.org/p/z-OUhvK7Kk

图片转代码服务由CSDN问答提供 功能建议

尝试在Go中将字符串数组转换为json字符串。 但是我得到的只是一个数字数组。

我缺少什么?

  package main 
 
import(
“  fmt“ 
” encoding / json“ 
)
 
func main(){
 var urls = [] string {
” http://google.com“,
” http:// facebook。  com“,
” http://youtube.com“,
” http://yahoo.com“,
” http://twitter.com“,
” http://live.com“  ,
} 
 
 urlsJson,_:= json.Marshal(urls)
 fmt.Println(urlsJson)
} 
   
 
 

Go代码 游乐场: http://play.golang.org/p/z-OUhvK7Kk

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongu189744321158 2014-07-11 03:27
    已采纳

    By marshaling the object, you are getting the encoding (bytes) that represents the JSON string. If you want the string, you have to convert those bytes to a string.

    fmt.Println(string(urlsJson))
    
    打赏 评论
  • dpf7891 2014-07-11 17:46

    Another way is to use directly os.Stdout.Write(urlsJson)

    打赏 评论

相关推荐 更多相似问题