简体   繁体   中英

Parse stringified JSON

I'm trying to parse stringified JSON with Go, but I cannot find anything that explains how do to efficient parsing.

Example 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\"}"
}

I usually parse json by casting it to a struct. like this:

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
}

However I do not know how to correctly parse this type of response. Any help?

Assuming the json you have added has a formatting issue for value of "minicart_content", below should help -

package main

import (
    "fmt"
    "encoding/json"
)

var myjson string = `{
    "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\"}"
}`

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

func main() {

    var j AutoGenerated
    if err := json.Unmarshal([]byte(myjson), &j); err != nil {
        fmt.Println(err)
    }
    fmt.Println(j.AddedProductJSON)
}

do it in two steps.

package main

import (
    "encoding/json"
    "fmt"
)

func main() {
    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"`
    }
    type addedProduct struct {
        ID   string `json:"id"`
        Size string `json:"size"`
    }
    type productRow struct {
        ProductID string `json:"productId"`
        Quantity  int    `json:"quantity"`
        UnitPrice string `json:"unitPrice"`
        Currency  string `json:"currency"`
        SKU       string `json:"sku"`
    }
    var j AutoGenerated

    body := []byte(`{
 "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\"}"
}`)

    if err := json.Unmarshal(body, &j); err != nil {
        panic(err)
    }

    var k []productRow
    if err := json.Unmarshal([]byte(j.Addcartrows), &k); err != nil {
        panic(err)
    }

    var u addedProduct
    if err := json.Unmarshal([]byte(j.AddedProductJSON), &u); err != nil {
        panic(err)
    }
    fmt.Printf("%#v\n\n", j)
    fmt.Printf("%#v\n\n", k)
    fmt.Printf("%#v\n\n", u)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM