如何将JSON解析为数据结构

I am trying to parse a JSON of the type

"{\"ids\":[\"a\",\"b\"]}"

Here is my code:

package main

import "fmt"
import "encoding/json"
import "strings"

type Idlist struct {
    id []string `json:"ids"`
}
func main() {
    var val []byte = []byte(`"{\"ids\":[\"a\",\"b\"]}"`)
    jsonBody, _ := strconv.Unquote(string(val))

    var holder Idlist
    if err := json.NewDecoder(strings.NewReader(jsonBody)).Decode(&holder); err!= nil{
        fmt.Print(err)
    }
    fmt.Print(holder)
    fmt.Print(holder.id)
}

However, I keep getting output

{[]}[]

I cannot get the data in the structure. Where am I going wrong? Here is the playground link: https://play.golang.org/p/82BaUlfrua

查看全部
doucheng1884
doucheng1884
2015/11/25 09:14
  • json
  • parsing
  • 点赞
  • 收藏
  • 回答
    私信
满意答案
查看全部

2个回复