dsbiw2911188 2015-11-19 14:14
浏览 84
已采纳

无法在Golang结构中处理JSON字符串数据

I have a json data in string (coming from third party API). I am not able to decode json string data in golang.

Please help.

JSON string =

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

Struct in which I want to get data

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"`
}

Here is my code where I want to process 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)
    }

Error I am getting : cannot range over *tmp (type PromoCacheData)

  • 写回答

1条回答 默认 最新

  • dqjo4340 2015-11-19 14:32
    关注

    You need to use tmp.Data in for loop instead of *tmp.
    Error message says that exactly

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,
  • ¥15 spaceclaim模型变灰色