解析字符串化 JSON

我正在尝试使用Go解析字符串化的JSON,但我找不到任何解释如何进行高效解析的东西。


示例 JSON:


{

 "messages":"<ul class=\"messages\"><li class=\"success-msg\"><ul><li><span>Item succcessfully added<\/span><\/li><\/ul><\/li><\/ul>",

 "app_cart":"[]",

 "addcartrows":"[{\"productId\":\"1675688\",\"quantity\":1,\"unitPrice\":\"290.00\",\"currency\":\"EUR\",\"sku\":\"P00525485-3\"}]",

 "minicart_content":"<ul class=\"checkout-types minicart\">\n

<li>",

 "cart_qty":"1",

 "added_product_json":"{\"id\":\"1675695\",\"size\":\"40\"}"

}


我通常通过将 json 转换为结构来解析 json。喜欢这个:


type AutoGenerated struct {

    Messages         string `json:"messages"`

    AppCart          string `json:"app_cart"`

    Addcartrows      string `json:"addcartrows"`

    MinicartContent  string `json:"minicart_content"`

    CartQty          string `json:"cart_qty"`

    AddedProductJSON string `json:"added_product_json"`

}

var j AutoGenerated 


if err = json.Unmarshal(body, &AutoGenerated); err != nil {

    fmt.Println(err) // json: cannot unmarshal string into Go struct field AutoGenerated.added_product_json

}


但是,我不知道如何正确解析这种类型的响应。有什么帮助吗?


GCT1015
浏览 61回答 2
2回答

慕容708150

分两步完成。package mainimport (&nbsp; &nbsp; "encoding/json"&nbsp; &nbsp; "fmt")func main() {&nbsp; &nbsp; type AutoGenerated struct {&nbsp; &nbsp; &nbsp; &nbsp; Messages&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;string `json:"messages"`&nbsp; &nbsp; &nbsp; &nbsp; AppCart&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string `json:"app_cart"`&nbsp; &nbsp; &nbsp; &nbsp; Addcartrows&nbsp; &nbsp; &nbsp; string `json:"addcartrows"`&nbsp; &nbsp; &nbsp; &nbsp; MinicartContent&nbsp; string `json:"minicart_content"`&nbsp; &nbsp; &nbsp; &nbsp; CartQty&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string `json:"cart_qty"`&nbsp; &nbsp; &nbsp; &nbsp; AddedProductJSON string `json:"added_product_json"`&nbsp; &nbsp; }&nbsp; &nbsp; type addedProduct struct {&nbsp; &nbsp; &nbsp; &nbsp; ID&nbsp; &nbsp;string `json:"id"`&nbsp; &nbsp; &nbsp; &nbsp; Size string `json:"size"`&nbsp; &nbsp; }&nbsp; &nbsp; type productRow struct {&nbsp; &nbsp; &nbsp; &nbsp; ProductID string `json:"productId"`&nbsp; &nbsp; &nbsp; &nbsp; Quantity&nbsp; int&nbsp; &nbsp; `json:"quantity"`&nbsp; &nbsp; &nbsp; &nbsp; UnitPrice string `json:"unitPrice"`&nbsp; &nbsp; &nbsp; &nbsp; Currency&nbsp; string `json:"currency"`&nbsp; &nbsp; &nbsp; &nbsp; SKU&nbsp; &nbsp; &nbsp; &nbsp;string `json:"sku"`&nbsp; &nbsp; }&nbsp; &nbsp; var j AutoGenerated&nbsp; &nbsp; body := []byte(`{&nbsp;"messages":"<ul class=\"messages\"><li class=\"success-msg\"><ul><li><span>Item succcessfully added<\/span><\/li><\/ul><\/li><\/ul>",&nbsp;"app_cart":"[]",&nbsp;"addcartrows":"[{\"productId\":\"1675688\",\"quantity\":1,\"unitPrice\":\"290.00\",\"currency\":\"EUR\",\"sku\":\"P00525485-3\"}]",&nbsp;"minicart_content":"<ul class=\"checkout-types minicart\">\n<li>",&nbsp;"cart_qty":"1",&nbsp;"added_product_json":"{\"id\":\"1675695\",\"size\":\"40\"}"}`)&nbsp; &nbsp; if err := json.Unmarshal(body, &j); err != nil {&nbsp; &nbsp; &nbsp; &nbsp; panic(err)&nbsp; &nbsp; }&nbsp; &nbsp; var k []productRow&nbsp; &nbsp; if err := json.Unmarshal([]byte(j.Addcartrows), &k); err != nil {&nbsp; &nbsp; &nbsp; &nbsp; panic(err)&nbsp; &nbsp; }&nbsp; &nbsp; var u addedProduct&nbsp; &nbsp; if err := json.Unmarshal([]byte(j.AddedProductJSON), &u); err != nil {&nbsp; &nbsp; &nbsp; &nbsp; panic(err)&nbsp; &nbsp; }&nbsp; &nbsp; fmt.Printf("%#v\n\n", j)&nbsp; &nbsp; fmt.Printf("%#v\n\n", k)&nbsp; &nbsp; fmt.Printf("%#v\n\n", u)}

缥缈止盈

假设您添加的 json 存在值“minicart_content”的格式问题,则可能会有所帮助 -package mainimport (&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "encoding/json")var myjson string = `{&nbsp; &nbsp; "messages": "<ul class=\"messages\"><li class=\"success-msg\"><ul><li><span>Item succcessfully added<\/span><\/li><\/ul><\/li><\/ul>",&nbsp; &nbsp; "app_cart": "[]",&nbsp; &nbsp; "addcartrows": "[{\"productId\":\"1675688\",\"quantity\":1,\"unitPrice\":\"290.00\",\"currency\":\"EUR\",\"sku\":\"P00525485-3\"}]",&nbsp; &nbsp; "minicart_content": "<ul class=\"checkout-types minicart\">\n <li > ",&nbsp; &nbsp; "cart_qty": "1",&nbsp; &nbsp; "added_product_json": "{\"id\":\"1675695\",\"size\":\"40\"}"}`type AutoGenerated struct {&nbsp; &nbsp; Messages&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;string `json:"messages"`&nbsp; &nbsp; AppCart&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string `json:"app_cart"`&nbsp; &nbsp; Addcartrows&nbsp; &nbsp; &nbsp; string `json:"addcartrows"`&nbsp; &nbsp; MinicartContent&nbsp; string `json:"minicart_content"`&nbsp; &nbsp; CartQty&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string `json:"cart_qty"`&nbsp; &nbsp; AddedProductJSON string `json:"added_product_json"`}func main() {&nbsp; &nbsp; var j AutoGenerated&nbsp; &nbsp; if err := json.Unmarshal([]byte(myjson), &j); err != nil {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println(err)&nbsp; &nbsp; }&nbsp; &nbsp; fmt.Println(j.AddedProductJSON)}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go