Golang 读取一个 JSON 对象数组

huangapple go评论86阅读模式
英文:

Golang Reading an Array of Json objects

问题

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

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

我的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对象发送到端点。

谢谢你的帮助,对于我是个GoLang新手表示抱歉!

英文:

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]\n", 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

得分: 1

// 解析 JSON。
var objs interface{}
json.Unmarshal([]byte(jsonStr), &objs) // 或者使用 json.Decoder.Decode(...)

// 确保它是一个对象数组。
objArr, ok := objs.([]interface{})
if !ok {
log.Fatal("期望一个对象数组")
}

// 将每个对象处理为 map[string]interface{}。
for i, obj := range objArr {
obj, ok := obj.(map[string]interface{})
if !ok {
log.Fatalf("期望类型为 map[string]interface{},得到 %s", reflect.TypeOf(objArr[i]))
}
fmt.Printf("i=%d, o=%T\n", i, obj) // 对对象进行处理...
}

英文:
// 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...
}

huangapple
  • 本文由 发表于 2015年4月6日 08:22:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/29463791.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定