gpt4 book ai didi

json - 在 Go 中循环嵌套的 JSON 元素

转载 作者:IT王子 更新时间:2023-10-29 02:32:20 25 4
gpt4 key购买 nike

我从包含一个或多个“实体”的 API 收到 JSON 响应。 JSON 看起来像这样:

{
"3211": {
"entity_id": "3211",
"status": "complete",
"coupon_code": "COUPON",
"shipping_description": "Shipping - AU Courier",
"customer_id": "2775",
"base_discount_amount": "-50.0000",
"base_grand_total": "149.0000",
"base_shipping_amount": "0.0000",
"base_shipping_tax_amount": "0.0000",
"base_subtotal": "199.0000",
"base_tax_amount": "0.0000",
"base_total_paid": "149.0000",
"base_total_refunded": null,
"discount_amount": "-50.0000",
"grand_total": "149.0000",
"shipping_amount": "0.0000",
"shipping_tax_amount": "0.0000",
"store_to_order_rate": "1.0000",
"subtotal": "199.0000",
"tax_amount": "0.0000",
"total_paid": "149.0000",
"total_refunded": null,
"base_shipping_discount_amount": "0.0000",
"base_subtotal_incl_tax": "199.0000",
"base_total_due": "0.0000",
"shipping_discount_amount": "0.0000",
"subtotal_incl_tax": "199.0000",
"total_due": "0.0000",
"increment_id": "200000423",
"base_currency_code": "AUD",
"discount_description": "COUPON",
"remote_ip": "123.123.123.123",
"store_currency_code": "AUD",
"store_name": "Australia",
"created_at": "2017-07-17 03:07:40",
"shipping_incl_tax": "0.0000",
"payment_method": "ewayrapid_ewayone",
"gift_message_from": null,
"gift_message_to": null,
"gift_message_body": null,
"tax_name": null,
"tax_rate": null,
"addresses": [
{
"region": "South Australia",
"postcode": "5000",
"lastname": "Doe",
"street": "Level 6\n25 Example Street",
"city": "Adelaide",
"email": "example@email.com",
"telephone": "+61 123 456 789",
"country_id": "AU",
"firstname": "John",
"address_type": "billing",
"prefix": null,
"middlename": null,
"suffix": null,
"company": null
},
{
"region": "South Australia",
"postcode": "5000",
"lastname": "Doe",
"street": "Level 6\n25 Example Street",
"city": "Adelaide",
"email": "example@email.com",
"telephone": "+61 123 456 789",
"country_id": "AU",
"firstname": "John",
"address_type": "shipping",
"prefix": null,
"middlename": null,
"suffix": null,
"company": null
}
]
}
}

我编写了以下struct 类型:

type Orders map[string]Order
type Order struct {
EntityID string `json:"entity_id"`
Status string `json:"status"`
CouponCode string `json:"coupon_code"`
ShippingDescription string `json:"shipping_description"`
CustomerID string `json:"customer_id"`
BaseDiscountAmount string `json:"base_discount_amount"`
BaseGrandTotal string `json:"base_grand_total"`
BaseShippingAmount string `json:"base_shipping_amount"`
BaseShippingTaxAmount string `json:"base_shipping_tax_amount"`
BaseSubtotal string `json:"base_subtotal"`
BaseTaxAmount string `json:"base_tax_amount"`
BaseTotalPaid string `json:"base_total_paid"`
BaseTotalRefunded string `json:"base_total_refunded"`
DiscountAmount string `json:"discount_amount"`
GrandTotal string `json:"grand_total"`
ShippingAmount string `json:"shipping_amount"`
ShippingTaxAmount string `json:"shipping_tax_amount"`
StoreToOrderRate string `json:"store_to_order_rate"`
Subtotal string `json:"subtotal"`
TaxAmount string `json:"tax_amount"`
TotalPaid string `json:"total_paid"`
TotalRefunded string `json:"total_refunded"`
BaseShippingDiscountAmount string `json:"base_shipping_discount_amount"`
BaseSubtotalInclTax string `json:"base_subtotal_incl_tax"`
BaseTotalDue string `json:"base_total_due"`
ShippingDiscountAmount string `json:"shipping_discount_amount"`
SubtotalInclTax string `json:"subtotal_incl_tax"`
TotalDue string `json:"total_due"`
IncrementID string `json:"increment_id"`
BaseCurrencyCode string `json:"base_currency_code"`
DiscountDescription string `json:"discount_description"`
RemoteIP string `json:"remote_ip"`
StoreCurrencyCode string `json:"store_currency_code"`
StoreName string `json:"store_name"`
CreatedAt string `json:"created_at"`
ShippingInclTax string `json:"shipping_incl_tax"`
PaymentMethod string `json:"payment_method"`
TaxName string `json:"tax_name"`
TaxRate string `json:"tax_rate"`
Addresses map[string]OrderAddresses `json:"addresses"`
}

type OrderAddresses []struct {
Region string `json:"region"`
Postcode string `json:"postcode"`
Lastname string `json:"lastname"`
Street string `json:"street"`
City string `json:"city"`
Email string `json:"email"`
Telephone string `json:"telephone"`
CountryID string `json:"country_id"`
Firstname string `json:"firstname"`
AddressType string `json:"address_type"`
Prefix *string `json:"prefix"`
Middlename *string `json:"middlename"`
Suffix *string `json:"suffix"`
Company *string `json:"company"`
}

然后我尝试像这样处理它(getFromOrdersAPI(page) 函数在 Orders 类型中返回上面提到的 JSON):

for page := 1; page < 3; page++ {
orders := getFromOrdersAPI(page)
for _, order := range orders {
//Process all the order items except addresses
fmt.Println("Processing entity:", orders.EntityID)

for _, orderaddress := range order.Addresses {
//Trying to access address values - example below
fmt.Println(orderaddress.Region)
}
}
}

运行时出现错误:

orderaddress.Region undefined (type OrderAddresses has no field or method Region)

我哪里错了?

最佳答案

您收到的错误是由于您定义 OrderAddresses 结构的方式所致。而不是

type OrderAddresses []struct {

如果你使用

type OrderAddresses struct {

没有大括号,你的编译错误应该会消失。

也就是说,鉴于我正在查看的 blob,我不确定您的 json 是否会按照您的预期进行解析。看来 json blob 中的 addresses 是一个数组。这意味着不是将其表示为

Addresses map[string]OrderAddresses `json:"addresses"`

您应该简单地将其作为 OrderAddresses 的一部分

Addresses []OrderAddresses `json:"addresses"`

关于json - 在 Go 中循环嵌套的 JSON 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45686269/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com