Golang读取一组Json对象

时间:2015-04-06 00:22:37

标签: arrays json parsing go

我正在使用GoLang并希望读取该文件并能够将每个json对象发送到REST端点。

除了REST端点之外,我在解析文件时遇到问题。

  package main

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

    func main() {
        type myjson struct {
            myobjects []struct {
                data map[string]string
            }
        }
        file, e := ioutil.ReadFile("dat_one_extract.json")
        if e != nil {
            fmt.Printf("File Error: [%v]\n", e)
            os.Exit(1)
        }

        dec := json.NewDecoder(bytes.NewReader(file))
        var d myjson
        dec.Decode(&d)


    }

我的Json文件如下所示:

[{
     "NAME1": "Y",
    "NAME2": 1729.0,
    "NAME3": "Y",
    "NAME4": [
        {
             "Contact Zip": "33619",
            "Date of Contact": "01/21/2015"
        }
    ],
     "NAME6": "123456789",
    "NAME7": "Walmart",
    "NAME8": [
        {
            "State": "xx",
            "Zip Code": "12345",
            "Address": "12345 main street",
            "Type": "MAILING",
            "City": "MyTown"
        },
        {
            "State": "xx",
            "Zip Code": "12345",
            "Address": "12345 main street",
            "Type": "MAILING",
            "City": "MyTown"
        },
        {
            "State": "xx",
            "Zip Code": "12345",
            "Address": "12345 main street",
            "Type": "MAILING",
            "City": "MyTown"
        }
    ],
    "NAME11": "XXXXXXX",
    "NAME12": "11/21/2014 14:01:47",
    "NAME13": "11/15/2014",
    "NAME14": "11/16/1992"
},{
    "NAME1": "Y",
    "NAME2": 1729.0,
    "NAME3": "Y",
    "NAME4": [
        {
             "Contact Zip": "33619",
            "Date of Contact": "01/21/2015"
        }
    ],
     "NAME6": "123456789",
    "NAME7": "Walmart",
    "NAME8": [
        {
            "State": "xx",
            "Zip Code": "12345",
            "Address": "12345 main street",
            "Type": "MAILING",
            "City": "MyTown"
        },
        {
            "State": "xx",
            "Zip Code": "12345",
            "Address": "12345 main street",
            "Type": "MAILING",
            "City": "MyTown"
        },
        {
            "State": "xx",
            "Zip Code": "12345",
            "Address": "12345 main street",
            "Type": "MAILING",
            "City": "MyTown"
        }
    ],
    "NAME11": "XXXXXXX",
    "NAME12": "11/21/2014 14:01:47",
    "NAME13": "11/15/2014",
    "NAME14": "11/16/1992"
}]

访问每个数组元素并将其传递给终点时我缺少什么? 我不想处理元素,只是将json对象发送到端点1。

提前感谢您,并为成为golang noob而道歉!

1 个答案:

答案 0 :(得分:0)

// Parse the JSON.
var objs interface{}
json.Unmarshal([]byte(jsonStr), &objs) // Or use json.Decoder.Decode(...)

// Ensure that it is an array of objects.
objArr, ok := objs.([]interface{})
if !ok {
    log.Fatal("expected an array of objects")
}

// Handle each object as a map[string]interface{}.
for i, obj := range objArr {
    obj, ok := obj.(map[string]interface{})
    if !ok {
        log.Fatalf("expected type map[string]interface{}, got %s", reflect.TypeOf(objArr[i]))
    }
    fmt.Printf("i=%d, o=%T\n", i, obj) // Do something with the object...
}