duaedf6509 2015-04-06 00:22
浏览 336

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!

  • 写回答

1条回答 默认 最新

  • doulaozhi6835 2015-04-06 01:07
    关注
    // 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...
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!