Golang读取Json对象数组

I am using GoLang and want to read the file and be able to send each json object to a REST Endpoint.

the REST endpoint aside, I am having issues parsing the file.

  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]
", e)
            os.Exit(1)
        }

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


    }

My Json file looks like this:

[{
     "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"
}]

What am I missing to access each array element and pass it to the end point? I don't want to process the elements, just send the json object to the endpoint 1 at time.

Thank you in advance and apologies for being a golang noob!

dq804806
dq804806 永远不要忽略错误(例如来自解码)。
5 年多之前 回复
dounao2829
dounao2829 我的结构的可能重复项没有编组到json中
5 年多之前 回复

1个回答

// 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
", i, obj) // Do something with the object...
}
duanchuan6350
duanchuan6350 这个答案可能有帮助-stackoverflow.com/questions/29421470/…
5 年多之前 回复
dongzhan5246
dongzhan5246 谢谢你的回答。 我可以在Array和映射中读取文件及其内容。 我对解决方案有疑问/挑战:1.该文件是摘录,具有15,000 + Objs; 因此,该文件约为50兆。 在执行任何操作之前,我不想将整个文件读入内存。 那么,我如何一次读取一个obj? 再次感谢你。
5 年多之前 回复
douxuanwei1980
douxuanwei1980 为什么要下票?
5 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问