dongle3217 2015-06-30 06:31
浏览 194
已采纳

解组嵌套的json字符串使用json.RawMessage

I'm having trouble wrapping my head around how to unmarshal a raw json string that was from a previously unmarshaled json byte array. I have the current code:

type Message struct {
    Event string
    Data json.RawMessage  // how data is parsed depends on the event
}

type CreateMessage struct {
    id int
}

var evt = []byte(`{"event": "create", "data" :{"id":5 }}`)

func main() {
    var message Message
    log.Println(string(evt))
    json.Unmarshal(evt, &message)

    log.Println(message)
    log.Println(message.Event)
    log.Println(string(message.Data))
    fmt.Printf("%+v
", message)

    var message2 = new(CreateMessage)
    err := json.Unmarshal( message.Data, &message2 )

    log.Println(message2)
    log.Println(err)
}

And the output is:

2015/06/29 23:22:10 {"event": "create", "data" :{"id":5 }}
2015/06/29 23:22:10 {create [123 34 105 100 34 58 53 32 125]}
2015/06/29 23:22:10 create
2015/06/29 23:22:10 {"id":5 }
{Event:create Data:[123 34 105 100 34 58 53 32 125]}
2015/06/29 23:22:10 &{0}
2015/06/29 23:22:10 <nil>

Why can't I unmarshal data as a CreateMessage object? I tried the example here and here but they don't unmarshal the nested raw json data and that is exactly what I'm trying to do.

  • 写回答

1条回答 默认 最新

  • duanlie2709 2015-06-30 06:54
    关注

    The problem is simply that the id field of the CreateMessage struct is unexported, it starts with a lowercase letter. Change it to:

    type CreateMessage struct {
        Id int
    }
    

    And it will work.

    Notes:

    Since message2 is already a pointer (new(CreateMessage)), you don't have to pass its address to json.Unmarshal(), its value is enough:

    var message2 = new(CreateMessage)
    if err := json.Unmarshal(message.Data, message2); err != nil {
        panic(err)
    }
    log.Printf("%+v", message2)
    

    Output:

    2009/11/10 23:00:00 &{Id:5}
    

    Or don't use new() at all:

    var message2 CreateMessage
    if err := json.Unmarshal(message.Data, &message2); err != nil {
        panic(err)
    }
    log.Printf("%+v", message2)
    

    Output:

    2009/11/10 23:00:00 {Id:5}
    

    Try it on the Go Playground.

    Also note that now the name of the field is "Id" and JSON contains "id" but the json package is "intelligent" enough to match them (same with "Event" and "event"). But know that if you would ever try to marshal your structs, the output would contain "Id" and not "id".

    If you want to use a completely different field name or make sure it will be lowercased when you marshal your structs, you can use struct tags to tell how it should appear in the JSON text, for example:

    type CreateMessage struct {
        MyId int `json:"id"`
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果