Golang的JSON解析

huangapple go评论76阅读模式
英文:

Golang json parsing

问题

我有一个像这样的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库进行解组时,我无法使其工作(错误始终是无法将JSON数据解组到main.DisassembleOutput结构体中)。
顺便说一下,我是一个Go语言新手。

编辑:

这实际上是问题所在:http://play.golang.org/p/cpYsnXa-b9。
如何逐个解析[{ ... }]中的项?

英文:

I have a json string like this:

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

and i have created a struct

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

i just want to save this json into the struct like this one.
So the problem is in unmarshaling with encoding/json lib, i can't get it to work ( error is always unable to unmarshal json data into main.Disas structure).
I'm a golang newbie btw.
<br><br>
EDIT:

This is actually the problem http://play.golang.org/p/cpYsnXa-b9.
how to parse one by one items fromm [{ ... }]

答案1

得分: 1

将字段名称以大写字母开头进行导出:

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

playground示例

英文:

Export the field names by starting them with an uppercase letter:

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

playground example

huangapple
  • 本文由 发表于 2015年11月22日 01:27:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/33846393.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定