Created
August 24, 2015 02:58
-
-
Save ikbear/ac41f900bda6c4456857 to your computer and use it in GitHub Desktop.
JSON 解析出错
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
Name string
json:"name"``
=>
Name string
json:name``
// 包裹 name 的双引号去掉也不能报错,但就是解析不成功。
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
json: "repo_info"
// "json:" 后面不能带有空格,否则不会报错,也不会解析成功