Skip to content

Instantly share code, notes, and snippets.

@ikbear
Created August 24, 2015 02:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ikbear/ac41f900bda6c4456857 to your computer and use it in GitHub Desktop.
Save ikbear/ac41f900bda6c4456857 to your computer and use it in GitHub Desktop.
JSON 解析出错
package main
import (
"encoding/json"
"fmt"
)
func main() {
var jsonBlob = []byte(`{
"repo_info": {
"name": "qiniu-nginx",
"description": "这是一个测试的七牛 Nginx 镜像",
"tags": [ {
"name": "latest",
"id": "6886fb5a9b8d"
}]
},
"pull_command": "docker pull index.qcos.me/library/qiniu-nginx"
}`)
type Tag struct {
Name string `json:"name"`
Id string `json:"id"`
}
type Repo struct {
Name string `json:"name"`
Description string `json:"description"`
Tags []*Tag `json:"tags,omitempty"`
}
type RecommendRepo struct {
RepoInfo Repo `json: "repo_info"` // "json:" 后面不能带有空格,否则不会报错,也不会解析成功
PullCommand string `json: "pull_command"`
}
var res RecommendRepo
err := json.Unmarshal(jsonBlob, &res)
if err != nil {
fmt.Println("error:", err)
}
fmt.Printf("%+v", res)
}
@ikbear
Copy link
Author

ikbear commented Aug 24, 2015

json: "repo_info"

// "json:" 后面不能带有空格,否则不会报错,也不会解析成功

@ikbear
Copy link
Author

ikbear commented Aug 26, 2015

Name stringjson:"name"``

=>

Name stringjson:name``

// 包裹 name 的双引号去掉也不能报错,但就是解析不成功。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment