json - 无法处理 golang 结构中的 json 字符串数据

标签 json go

我有一个字符串形式的 json 数据(来自第三方 API)。我无法在 golang 中解码 json 字符串数据。

请帮忙。

JSON 字符串 =

{
        "data" : {
                 "additional-30":  {
                       "id_sales_rule_set": 255626,
                        "voucher_code": "PR35ZR5J5",
                         "from_date": "2015-06-16 16:19:22",
                         "to_date": "2018-09-28 23:59:59",
                         "conditions_ruleset": {
                             "subTotal": 0,
                              "category": {},
                              "customer": "0",
                              "paymentMethod": null,
                              "capOnDiscount": null,
                              "skuExclude": null,
                              "discountedItem": 0,
                              "discounted": 1500,
                              "taggedItem": null,
                              "segmentedVoucher": null,
                              "bundle": null,
                              "brand": null,
                              "mobileVoucher": null,
                              "itemAttribute": {}
                         },
                        "discount_type": "fixed",
                        "discount_percentage": null,
                        "discount_amount_default": 500
        },
        "abcd":  {
                       "id_sales_rule_set": 255626,
                        "voucher_code": "PR35ZR5J5",
                         "from_date": "2015-06-16 16:19:22",
                         "to_date": "2018-09-28 23:59:59",
                         "conditions_ruleset": {
                             "subTotal": 0,
                              "category": {},
                              "customer": "0",
                              "paymentMethod": null,
                              "capOnDiscount": null,
                              "skuExclude": null,
                              "discountedItem": 0,
                              "discounted": 1500,
                              "taggedItem": null,
                              "segmentedVoucher": null,
                              "bundle": null,
                              "brand": null,
                              "mobileVoucher": null,
                              "itemAttribute": {}
                         },
                        "discount_type": "fixed",
                        "discount_percentage": null,
                        "discount_amount_default": 500
        } 
    }
}

我要获取数据的结构

type ConditionsRuleset struct {
        Brand            interface{} `json:"brand"`
        Bundle           interface{} `json:"bundle"`
        CapOnDiscount    interface{} `json:"capOnDiscount"`
        Category         struct{}    `json:"category"`
        Customer         string      `json:"customer"`
        Discounted       int         `json:"discounted"`
        DiscountedItem   int         `json:"discountedItem"`
        ItemAttribute    struct{}    `json:"itemAttribute"`
        MobileVoucher    interface{} `json:"mobileVoucher"`
        PaymentMethod    interface{} `json:"paymentMethod"`
        SegmentedVoucher interface{} `json:"segmentedVoucher"`
        SkuExclude       interface{} `json:"skuExclude"`
        SubTotal         int         `json:"subTotal"`
        TaggedItem       interface{} `json:"taggedItem"`
    }

type PromoVoucher struct {
    ConditionsRuleset ConditionsRuleset `json:"conditions_ruleset"`
    DiscountAmountDefault int         `json:"discount_amount_default"`
    DiscountPercentage    interface{} `json:"discount_percentage"`
    DiscountType          string      `json:"discount_type"`
    FromDate              string      `json:"from_date"`
    IDSalesRuleSet        int         `json:"id_sales_rule_set"`
    ToDate                string      `json:"to_date"`
    VoucherCode           string      `json:"voucher_code"`
}

type PromoCacheData struct {
    Data map[string]interface{} `json:"data"`
}

这是我要处理json的代码

by := []byte(<json string>)
    tmp := new(PromoCacheData)
    json.Unmarshal(by,tmp)

    for k,value := range *tmp {

        byc, _ := json.Marshal(value)
        tmp2 := new(PromoVoucher)
        json.Unmarshal(byc,tmp2)
        fmt.Println(tmp2)
    }

我得到的错误:cannot range over *tmp (type PromoCacheData)

最佳答案

您需要在 for 循环中使用 tmp.Data 而不是 *tmp
错误消息正是这样说的

关于json - 无法处理 golang 结构中的 json 字符串数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33806321/

相关文章:

java - 如何使用 Java 在屏幕上打印 JSON 文件的值?

go - Go 中的惯用 slice 拼接?

binary - 从 stdin 接收二进制数据,发送到 Go 中的 channel

http - 如何在 go-chi 中启用 gzip 压缩中间件

go - 在 Go 中分配大字符串的最快方法?

java - 如何使用 json 将文件对象发送到 servlet

javascript - Backbone.js - 让 json 数据进入 View

go - 长时间运行复制到 bytes.Buffer

json - Mailchimp API 为新成员和现有成员添加标签

javascript - get ('url' ) 使用 AngularJS 和 ServiceStack webservice 进行操作