【问题标题】:Golang json parsing [duplicate]Golang json解析[重复]
【发布时间】:2016-02-24 02:30:12
【问题描述】:

我有一个这样的 json 字符串:

    {
"offset":4224368,
"fcn_addr":4224368,
"fcn_last":4224408,
"size":2,
"opcode":"add byte [rax], al",
"bytes":"0000",
"type":"add",
"type_num":17,
"type2_num":0,
"flags":[
"entry0",
"sym._start",
"section_end..plt",
"section..text"
],
"comment":"WzEzXSB2YT0weDAwNDA3NTcwIHBhPTB4MDAwMDc1NzAgc3o9MTA5OTE4NiB2c3o9MTA5OTE4NiByd3g9LXIteCAudGV4dA=="
},
{
"offset":4224370,
"fcn_addr":4224368,
"fcn_last":4224408,
"size":2,
"opcode":"add byte [rax], al",
"bytes":"0000",
"type":"add",
"type_num":17,
"type2_num":0
}}

我已经创建了一个结构

type DisassembleOutput struct {
    offset, fcn_addr, fcn_last, size int
    opcode, bytes string
}

我只想将此 json 保存到像这样的结构中。 所以问题在于使用 encoding/json lib 解组,我无法让它工作(错误总是无法将 json 数据解组到 main.Disas 结构)。 顺便说一句,我是一个 golang 新手。

编辑:

这实际上是问题http://play.golang.org/p/cpYsnXa-b9。 如何从 [{ ... }] 中逐一解析项目

【问题讨论】:

    标签: json encoding go


    【解决方案1】:

    Export 以大写字母开头的字段名称:

    type DisassembleOutput struct {
      Offset, Fcn_addr, Fcn_last, Size int
      Opcode, Bytes                    string
    }
    

    playground example

    【讨论】:

    • 这很有帮助,谢谢!还有一件事,如果json是这样的[{samejson here}]我该怎么办?再次感谢!
    • 我已经更新了 play golang 上的代码,其中包含关于我上次评论的实际问题,这里是链接 play.golang.org/p/cpYsnXa-b9
    • 您在操场上的示例不是有效的 JSON,如错误消息所示。不确定是什么意思。
    • 这是一个有效的 json,我找到了使用 mholt.github.io/json-to-go 的解决方案。无论如何感谢您的回答!
    • 使用 jsonlint.com 确保 JSON 有效。
    猜你喜欢
    • 2013-08-27
    • 2019-02-20
    • 1970-01-01
    • 1970-01-01
    • 2018-09-27
    • 1970-01-01
    • 2012-04-10
    • 2017-01-09
    • 1970-01-01
    相关资源
    最近更新 更多