使用字符串作为结构值

时间:2017-04-23 07:15:27

标签: go

我有这个代码。我需要的是从区块链返回的交易ID中获取交易详情

    package main

    import (
        "encoding/base64"
        "encoding/json"
        "fmt"
        "io/ioutil"
        "log"
        "net/http"
        "strings"
    )

    type Transaction struct {
        Bid               string `json:"bid"`
        Fun               string `json:"fun"`
        ID                string `json:"id"`
        Timestamp         string `json:"timestamp"`
        TraderA           string `json:"traderA"`
        TraderB           string `json:"traderB"`
        Seller            string `json:"seller"`
        PointAmount       string `json:"pointAmount"`
        PrevTransactionID string `json:"prevTransactionId"`
    }

    type AllTxs struct {
        TXs []Transaction `json:"tx"`
    }
    type Transact struct {
        Cert        string `json:"cert"`
        ChaincodeID string `json:"chaincodeID"`
        Nonce       string `json:"nonce"`
        Payload     string `json:"payload"`
        Signature   string `json:"signature"`
        Timestamp   string `json:"nanos"`
        Txid        string `json:"txid"`
        Type        int    `json:"type"`
    }

    func main() {
        resp, err := http.Get("http://blockchain_transactions_url/trans_id")
        if err != nil {
            // handle error
        }
        defer resp.Body.Close()
        body, err := ioutil.ReadAll(resp.Body)
        if err != nil {
            log.Fatal(err)
        }
        byteArray := []byte(body)
        var t Transact
        json.Unmarshal(byteArray, &t)
        //I get all the values base64 encoded
        st, err := base64.StdEncoding.DecodeString(t.Payload)
        if err != nil {
            log.Fatal(err)
        }
        trd := string(st)
        sp := strings.Split(trd, "\n")
        result := strings.Join(sp, ",")
        res := strings.Replace(result, ",", `", "`, -1)
        ret := strings.Replace(res, `", "`, `{"`, 1) + `"}`

        byteA := []byte(ret)
        var tf AllTxs
        json.Unmarshal(byteA, &tf)
        //the tf... 
        ref := Transaction{}

        fmt.Println(ref.Id)

    }

我得到的t.Payload是

“CsYBCAESgwESgAE3ZjFhY2Y2MTgxMGRhODMyMTA5NjZiNGYzNjc2NWU5NmIxY2Q0OTliODkyNmY0MDU0YWQ5NzhlNzhkZjczMDRhOGZlMDM1ZjZhYTBhODE2YzdmNjFlNGZkZDQ1MjM4M2Q5ZmU5ZDQxNmIyZGI4YTE1YmRkMjAzZmU2N2I5OTYyZho8ChBpbml0X3RyYW5zYWN0aW9uCgYwMDAxMTcKA2dpbwoEbW9oYQoBNQoCMTIKBTk4NzczCgcyMDE3NDIy”

我得到的是

{ “?? 7f1acf61810da83210966b4f36765e96b1cd499b8926f4054ad978e78df7304a8fe035f6aa0a816c7f61e4fdd452383d9fe9d416b2db8a15bdd203fe67b9962f<”, “init_transaction”, “000117”, “GIO”, “莫哈”, “5”, “12”, “98773”, “2017422”}

最后如何获得Transaction / AllTxs类型的JSON?

1 个答案:

答案 0 :(得分:0)

鉴于评论,看起来您正在尝试将字符串解组为JSON,但就目前而言,该字符串是无效的JSON;如果是的话,你可以按照以下方式解组它,忽略了这些字段似乎与所需结构不匹配的事实;

package main

import (
    "encoding/json"
    "fmt"
)

type Transaction struct {
    Bin               string `json:"bin"`
    Fun               string `json:"fun"`
    ID                string `json:"id"`
    Timestamp         string `json:"timestamp"`
    TraderA           string `json:"traderA"`
    TraderB           string `json:"traderB"`
    Seller            string `json:"seller"`
    PointAmount       string `json:"pointAmount"`
    PrevTransactionID string `json:"prevTransactionId"`
}

func main() {
    snippet := `{
        "bin": "7f1acf61810da83210966b4f36765e96b1cd499b8926f4054ad978e78df7304a8fe035f6aa0a816c7f61e4fdd452383d9fe9d416b2db8a15bdd203fe67b9962f",
        "fun": "init_transaction",
        "id": "000117",
        "timestamp": "gio",
        "traderA": "moha",
        "traderB": "5",
        "seller": "12",
        "pointAmount": "98773",
        "prevTransactionId": "2017422"
        }`
    t := Transaction{}
    json.Unmarshal([]byte(snippet), &t)
    fmt.Println(t)
}