Go JSON map 新手

huangapple go评论70阅读模式
英文:

Go JSON map newbie

问题

如何使这个小程序工作?我试图从一个 JSON 字符串中读取一堆 cookie,并将其存储到一个 map 中并打印出来。下面的程序没有打印任何内容。

type htmlDoc struct {
    cookies map[string]string `json:"Cookies"`
}

func main() {
    jsonString := `{
        "Cookies": {
            "name1": "Value1",
            "name2": "Value2",
            "name3": "Value3"
        }
    }`

    var doc htmlDoc
    json.Unmarshal([]byte(jsonString), &doc)

    for name, value := range doc.cookies {
        fmt.Printf("%s\t%s\n", name, value)
    }
}

请注意,我已经对代码进行了一些修正:

  1. json:"Cookies" 修改为 json:"Cookies",以正确标记 cookies 字段的 JSON 标签。
  2. &doc 修改为 &doc,以正确传递 doc 的指针给 json.Unmarshal 函数。
  3. 将 JSON 字符串中的方括号 [] 修改为大括号 {},以正确表示键值对的集合。

现在,这个程序应该能够正确地读取 JSON 字符串中的 cookie,并将其打印出来。

英文:

How would I get this little program to work? I'm trying to read a bunch of cookies from a json string into a map and print the map. The below program prints nothing.

type htmlDoc struct {
	cookies map[string] string `json:"Cookies"`
}

func main() {
    jsonString := `{    Cookies: {
        ["name1": "Value1"],
        ["name2": "Value2"],
        ["name3": "Value3"]
    }}`
     
    var doc htmlDoc
    json.Unmarshal([]byte(jsonString), &doc)

	for name, value := range doc.cookies {
		fmt.Printf("%s\t%s\n", name, value)
	}
}

答案1

得分: 1

你的代码中有一些错误,首先你的 JSON 是无效的,我相信预期的 JSON 应该是:

{"Cookies": [
    {"name1": "Value1"},
    {"name2": "Value2"},
    {"name3": "Value3"}]
}

此外,正如 md2perpe 所评论的,你必须从 htmlDoc 中导出 Cookies。另外,如果 Cookies 是一个映射数组,htmlDoc 必须具有以下结构:

type htmlDoc struct {
    Cookies []map[string]string `json:"Cookies"`
}

以及主函数:

func main() {
    jsonString := `{"Cookies": [
        {"name1": "Value1"},
        {"name2": "Value2"},
        {"name3": "Value3"}]}`

    var doc htmlDoc
    json.Unmarshal([]byte(jsonString), &doc)

    for _, value := range doc.Cookies {
        for k, v := range value {
            fmt.Printf("%s\t%s\n", k, v)
        }
    }
}

希望对你有帮助!

英文:

There are some errors in your code, first your json is invalid, I believe the expected JSON is:

{"Cookies": [
    {"name1": "Value1"},
    {"name2": "Value2"},
    {"name3": "Value3"}]
}

Also, as md2perpe commented, you have to export Cookies from htmlDoc. Another matter, if Cookies is an array of maps, the htmlDoc must have the following structure

type htmlDoc struct {
    Cookies []map[string]string `json:"Cookies"`
}

And the main func

func main() {
    jsonString := `{"Cookies": [
        {"name1": "Value1"},
        {"name2": "Value2"},
        {"name3": "Value3"}]}`

    var doc htmlDoc
    json.Unmarshal([]byte(jsonString), &doc)

    for _, value := range doc.Cookies {
        for k, v := range value {
            fmt.Printf("%s\t%s\n", k, v)
        }
    }
}

答案2

得分: 0

字段cookies需要被导出,即需要大写:

type htmlDoc struct {
    Cookies map[string]string `json:"Cookies"`
}
英文:

The field cookies need to be exported, i.e. be upper case:

type htmlDoc struct {
    Cookies map[string] string `json:"Cookies"`
}

huangapple
  • 本文由 发表于 2016年11月21日 09:45:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/40711643.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定