douxigai8757 2012-11-13 16:02
浏览 123
已采纳

如何正确解组不同类型的数组?

As long as I have key-value pairs unmarshalling is pretty straight forward but how would I unmarshall an array of different types in different order? The single elements are well defined and known but the order is not.

I can not come up with a beautiful solution.

Would I try and error over all elements? Is there some kind of union type that could do that for me?

playground version

package main

import (
    "encoding/json"
    "fmt"
)

var my_json string = `{
    "an_array":[
        "with_a string",
        {
            "and":"some_more",
            "different":["nested", "types"]
        }
    ]
}`

type MyInner struct {
    And     string
    Different   []string
}

type MyJSON struct {
    An_array []json.RawMessage
}

func main() {
    var my_json_test MyJSON

    e := json.Unmarshal([]byte(my_json), &my_json_test)
    if e != nil {
        fmt.Println(e)
    } else {
        for index, value := range my_json_test.An_array {
            fmt.Println("index: ", index)
            fmt.Println("value: ", string(value))
        }
        var my_inner MyInner
        err := json.Unmarshal(my_json_test.An_array[1], &my_inner)
        if err != nil {
            fmt.Println(err)
        } else {
            fmt.Println("inner structure: ", my_inner)
        }
    }
}
  • 写回答

1条回答 默认 最新

  • dongshen7561 2012-11-13 18:32
    关注

    Go official blog has a nice article about encoding/json: JSON and GO. It's possible to "Decode arbitrary data" into an interface{} and use type assertion to determine the type dynamically.

    Your code can be probably modified to this:

    package main
    
    import (
        "encoding/json"
        "fmt"
    )
    
    var my_json string = `{
        "an_array":[
        "with_a string",
        {
            "and":"some_more",
            "different":["nested", "types"]
        }
        ]
    }`
    
    func WTHisThisJSON(f interface{}) {
        switch vf := f.(type) {
        case map[string]interface{}:
            fmt.Println("is a map:")
            for k, v := range vf {
                switch vv := v.(type) {
                case string:
                    fmt.Printf("%v: is string - %q
    ", k, vv)
                case int:
                    fmt.Printf("%v: is int - %q
    ", k, vv)
                default:
                    fmt.Printf("%v: ", k)
                    WTHisThisJSON(v)
                }
    
            }
        case []interface{}:
            fmt.Println("is an array:")
            for k, v := range vf {
                switch vv := v.(type) {
                case string:
                    fmt.Printf("%v: is string - %q
    ", k, vv)
                case int:
                    fmt.Printf("%v: is int - %q
    ", k, vv)
                default:
                    fmt.Printf("%v: ", k)
                    WTHisThisJSON(v)
                }
    
            }
        }
    }
    
    func main() {
    
        fmt.Println("JSON:
    ", my_json, "
    ")
    
        var f interface{}
        err := json.Unmarshal([]byte(my_json), &f)
        if err != nil {
            fmt.Println(err)
        } else {
            fmt.Printf("JSON: ")
            WTHisThisJSON(f)
        }
    }
    

    It gives output as follows:

    JSON:
     {
        "an_array":[
        "with_a string",
        {
            "and":"some_more",
            "different":["nested", "types"]
        }
        ]
    } 
    
    JSON: is a map:
    an_array: is an array:
    0: is string - "with_a string"
    1: is a map:
    and: is string - "some_more"
    different: is an array:
    0: is string - "nested"
    1: is string - "types"
    

    It's not complete yet, but shows how it's gonna work.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?