簡體   English   中英

當左側部分是數​​字時如何在golang中解組json

[英]How to unmarshal json in golang when left part is a number

我想在代碼中解組這樣的json。 但是此代碼不起作用。 有什么建議么? 謝謝!

PS。 游樂場http://play.golang.org/p/m2f94LY_d_

package main

import "encoding/json"
import "fmt"

type Response struct {
    Page int
    One  string "1"
}

func main() {
    in := []byte(`{"page":1, "1":"this is 1"}`)
    res := &Response{}
    json.Unmarshal(in, &res)
    fmt.Println(res)
}

您需要告訴json庫什么是json字段名稱:

type Response struct {
    Page int `json:"page"`
    One  string `json:"1"`
}

直播: http//play.golang.org/p/CNcvQMqBGD

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM