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

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

Not able to process json string data in golang struct

问题

我有一个字符串形式的 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字符串>)
tmp := new(PromoCacheData)
json.Unmarshal(by, tmp)

for k, value := range tmp.Data {
    byc, _ := json.Marshal(value)
    tmp2 := new(PromoVoucher)
    json.Unmarshal(byc, tmp2)
    fmt.Println(tmp2)
}

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

英文:

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 =

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

Struct in which I want to get data

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

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

type PromoCacheData struct {
	Data map[string]interface{} `json:&quot;data&quot;`
}

Here is my code where I want to process json

by := []byte(&lt;json string&gt;)
	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

得分: 1

你需要在循环中使用tmp.Data而不是*tmp。错误信息确切地指出了这一点。

英文:

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

huangapple
  • 本文由 发表于 2015年11月19日 22:14:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/33806321.html
匿名

发表评论

匿名网友

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

确定