使用Golang的接口与Maps、Structs和JSON

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

Using Interfaces with Golang Maps and Structs and JSON

问题

我有一些 JSON 代码,可以看起来像这样:

{
    "message_id": "12345",
    "status_type": "ERROR",
    "status": {
        "x-value": "foo1234",
        "y-value": "bar4321"
    }
}

或者可能看起来像这样。如你所见,"status" 元素根据 "status_type" 的不同,从一个标准的字符串对象变成了一个字符串数组对象。

{
    "message_id": "12345",
    "status_type": "VALID",
    "status": {
        "site-value": [
            "site1",
            "site2"
        ]
    }
}

我认为我需要让我的 "Status" 结构体接受一个类似于 "map[string]interface{}" 的映射,但我不确定具体该如何做。

你也可以在这里的 playground 上查看代码。
http://play.golang.org/p/wKowJu_lng

package main

import (
	"encoding/json"
	"fmt"
)

type StatusType struct {
	Id     string            `json:"message_id,omitempty"`
	Status map[string]string `json:"status,omitempty"`
}

func main() {
	var s StatusType
	s.Id = "12345"
	m := make(map[string]string)
	s.Status = m
	s.Status["x-value"] = "foo1234"
	s.Status["y-value"] = "bar4321"

	var data []byte
	data, _ = json.MarshalIndent(s, "", "    ")

	fmt.Println(string(data))
}
英文:

I have some JSON code that can look like:

{
    "message_id": "12345",
    "status_type": "ERROR",
    "status": {
        "x-value": "foo1234",
        "y-value": "bar4321"
    }
}

or can look like this. As you can see the "status" element changes from a standard object of strings to an object of array of strings, based on the status_type.

{
    "message_id": "12345",
    "status_type": "VALID",
    "status": {
        "site-value": [
            "site1",
            "site2"
        ]
    }
}

I am thinking that I need to have my struct for "Status" take a map like "map[string]interface{}", but I am not sure exactly how to do that.

You can see the code here on the playground as well.
http://play.golang.org/p/wKowJu_lng

package main

import (
    "encoding/json"
    "fmt"
)

type StatusType struct {
    Id     string            `json:"message_id,omitempty"`
    Status map[string]string `json:"status,omitempty"`
}

func main() {
    var s StatusType
    s.Id = "12345"
    m := make(map[string]string)
    s.Status = m
    s.Status["x-value"] = "foo1234"
    s.Status["y-value"] = "bar4321"

    var data []byte
    data, _ = json.MarshalIndent(s, "", "    ")

    fmt.Println(string(data))
}

答案1

得分: 8

我已经弄清楚了,我想..

package main

import (
    "encoding/json"
    "fmt"
)

type StatusType struct {
    Id     string            `json:"message_id,omitempty"`
    Status map[string]interface{} `json:"status,omitempty"`
}

func main() {
    var s StatusType
    s.Id = "12345"
    m := make(map[string]interface{})
    s.Status = m

    // 现在这个可以工作
    // s.Status["x-value"] = "foo1234"
    // s.Status["y-value"] = "bar4321"

    // 这个也可以工作
    sites := []string{"a", "b", "c", "d"}
    s.Status["site-value"] = sites

    var data []byte
    data, _ = json.MarshalIndent(s, "", "    ")

    fmt.Println(string(data))
}
英文:

I figured it out, I think..

package main

import (
    "encoding/json"
    "fmt"
)

type StatusType struct {
    Id     string            `json:"message_id,omitempty"`
    Status map[string]interface{} `json:"status,omitempty"`
}

func main() {
    var s StatusType
    s.Id = "12345"
    m := make(map[string]interface{})
    s.Status = m

    // Now this works
    // s.Status["x-value"] = "foo1234"
    // s.Status["y-value"] = "bar4321"

    // And this works
    sites := []string{"a", "b", "c", "d"}
    s.Status["site-value"] = sites

    var data []byte
    data, _ = json.MarshalIndent(s, "", "    ")

    fmt.Println(string(data))
}

huangapple
  • 本文由 发表于 2015年4月2日 10:18:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/29403962.html
匿名

发表评论

匿名网友

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

确定