JSON key 可以是字符串或对象

Phi*_*ipp 4 json decode go

我想解析一些 JSON,但其中一个键要么是字符串,要么是对象。

这是我当前的结构:https://github.com/PhillippOhlandt/pmtoapib/blob/master/CollectionItemRequest.go#L10

type CollectionItemRequest struct {
    Url         string          `json:"url"`
    Method      string          `json:"method"`
    Header      []RequestHeader `json:"header"`
    Body        RequestBody     `json:"body"`
    Description string          `json:"description"`
}
Run Code Online (Sandbox Code Playgroud)

这里的“Url”属性不仅可以是字符串,还可以是对象。

我开始为它创建一个自己的结构来覆盖对象情况。

type CollectionItemRequestUrl struct {
    Raw string `json:"raw"`
}

type CollectionItemRequest struct {
    Url         CollectionItemRequestUrl `json:"url"`
    Method      string                   `json:"method"`
    Header      []RequestHeader          `json:"header"`
    Body        RequestBody              `json:"body"`
    Description string                   `json:"description"`
}
Run Code Online (Sandbox Code Playgroud)

但是字符串版本将不再起作用。有没有办法让这两种情况都工作并通过 getter 获取值,例如request.Url.Get

编辑:

以下是 JSON 的两个版本:

    "request": {
        "url": {
            "raw": "http://localhost:8081/users?per_page=5&page=2",
            "protocol": "http",
            "host": [
                "localhost"
            ],
            "port": "8081",
            "path": [
                "users"
            ],
            "query": [
                {
                    "key": "per_page",
                    "value": "5",
                    "equals": true,
                    "description": ""
                },
                {
                    "key": "page",
                    "value": "2",
                    "equals": true,
                    "description": ""
                }
            ],
            "variable": []
        },
Run Code Online (Sandbox Code Playgroud)

"request": {
                "url": "http://localhost:8081/users/2",
Run Code Online (Sandbox Code Playgroud)

注意:只有子集,整个 JSON 会太长。

Iai*_*can 5

string拥有一个具有自定义解组方法的类型,该方法将首先解组到一个空接口,然后对其是否有 a或 a进行类型切换,map[string]interface{}如下所示:

type example struct {
    URL myURL `json:"url"`
}

type myURL struct {
    url string
}

func (u *myURL) MarshalJSON() ([]byte, error) {
    return json.Marshal(u.url)
}

func (u *myURL) UnmarshalJSON(data []byte) error {
    var raw interface{}
    json.Unmarshal(data, &raw)
    switch raw := raw.(type) {
    case string:
        *u = myURL{raw}
    case map[string]interface{}:
        *u = myURL{raw["raw"].(string)}
    }
    return nil
}

const myStringURL string = `{"url": "http://www.example.com/as-string"}`
const myNestedURL string = `{"url": {"raw": "http://www.example.com/as-nested"}}`

func main() {
    var stringOutput example
    json.Unmarshal([]byte(myStringURL), &stringOutput)
    fmt.Println(stringOutput)

    var nestedOutput example
    json.Unmarshal([]byte(myNestedURL), &nestedOutput)
    fmt.Println(nestedOutput)
}
Run Code Online (Sandbox Code Playgroud)

可在此处的 go Playground 中运行:

https://play.golang.org/p/I6KC4aXHpxm