我在解组以下json时遇到了麻烦:

{
    "input-1": [
        "S",
        15.0,
        "some1",        
        [
            [
                1111111.357,
                "QQ",
                "Toronto"
            ]
        ]
    ],
    "input-2": [
        "EE",
        90.1,
        "some2",
        [
            [
                55555550.705,
                "RR",
                "Kuala Lumpur"
            ],
            [
                22233330.134,
                "OO",
                "Bogota"
            ]           
        ]
    ],
    "input-3": [
        "UU",
        87.0,
        "some99",       
        [
            [
                8800221.500,
                "LL",
                "Lagos"
            ]
        ]
    ]
}

我相应的代码如下所示:

package main

import (
    "fmt"
    "encoding/json"
    "os"
    "io/ioutil"
)

type InputC struct {
    Type                string
    SomeFloat           float64
    CorrespondingPerson string
    Desc                []Under
}
type Under struct {
    IDCity   float64
    CityType string
    CityName string
}
func main() {
    f, err := os.OpenFile("data.json", os.O_RDONLY, 0755)
    if err != nil {
        fmt.Printf("Error opening file: %v\n", err)
    }

    b, err := ioutil.ReadAll(f)
    if err != nil {
        fmt.Printf("Error reading file: %v\n", err)
    }

    inputs:= map[string]InputC{}
    err = json.Unmarshal(b, &inputs)
    if err != nil {
        fmt.Printf("Error Unmarshalling file: %v\n", err)
    }

    for k, v := range(inputs) {
        fmt.Printf("%v\n",v.Desc[0].IDCIty)
    }
}

我从此代码收到的响应是:

Error Unmarshalling file: json: cannot unmarshal array into Go value of type main.InputC panic: runtime error: index out of range

之后,我将代码更改为此:

    inputs:= []InputC{}

但仍然出现错误:

Error Unmarshalling file: json: cannot unmarshal object into Go value of type []main.InputC

很明显,我缺少了一些东西,但是我不知道如何从已有的JSON中提取信息。

分析解答

如果在自定义结构上实现json.Unmarshaler接口并使用[]json.RawMessage临时保存各个数组元素的值,则可以避免使用[]interface{}。请记住,下面的解决方案在很大程度上取决于要保证的数组值的数量和顺序。

type InputC struct {
    Type                string
    SomeFloat           float64
    CorrespondingPerson string
    Desc                []Under
}

func (c *InputC) UnmarshalJSON(data []byte) error {
    var s []json.RawMessage
    if err := json.Unmarshal(data, &s); err != nil {
        return err
    }
    if len(s) != 4 {
        return errors.New("something's off")
    }

    dest := []interface{}{&c.Type, &c.SomeFloat, &c.CorrespondingPerson, &c.Desc}
    for i := 0; i < len(s); i++ {
        if err := json.Unmarshal(s[i], dest[i]); err != nil {
            return err
        }
    }
    return nil
}

type Under struct {
    IDCity   float64
    CityType string
    CityName string
}

func (u *Under) UnmarshalJSON(data []byte) error {
    var s []json.RawMessage
    if err := json.Unmarshal(data, &s); err != nil {
        return err
    }
    if len(s) != 3 {
        return errors.New("something's off")
    }

    dest := []interface{}{&u.IDCity, &u.CityType, &u.CityName}
    for i := 0; i < len(s); i++ {
        if err := json.Unmarshal(s[i], dest[i]); err != nil {
            return err
        }
    }
    return nil
}

https://play.golang.com/p/qpnvETC18rp

(请注意,在操场上打印的浮点图看起来只是有所不同,因为在使用%v动词时,这就是包fmt在默认情况下打印浮点图的方式。例如,此fmt.Printf("%v", 1111111.357)输出1.111111357e+06。)