如何解析具有不同类型的JSON-RPC表格

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

How to parse JSON-RPC table with different type

问题

我想要使用以下结构从JSON-RPC文件中获取信息:

{
    "id": "foo1",
    "error": null,
    "result": [
        {
            "key": [
                "hello 1",
                1,
                "world 1"
            ],
            "val": {
                "type": "static"
            }
        },
        {
            "key": [
                "hello 2",
                1,
                "world 2"
            ],
            "val": {
                "type": "static"
            }
        }
    ]
}

这是我的解析函数,其中Key是字符串数组(不能接受int类型):

type JsonRpcRsp struct {
    Id     string          `json:"id"`
    Error  *string         `json:"error"`
    Result json.RawMessage `json:"result"`
}

type JsonRpcEntry_Val struct {
    Type     string          `json:"type"`
}
type JsonRpcEntry struct {
    Key     [3]string          `json:"key"`
    Val     JsonRpcEntry_Val  `json:"val"`
}

jsonResult := JsonRpcRsp{}
json.Unmarshal(data, &jsonResult)
entries := []JsonRpcEntry{}
for _, val := range jsonResult {
    json.Unmarshal(val.Result, &entries)
}

如何解析"key"数组?问题在于它包含不同类型的元素。

"key"数组的结构如下:

[<string>, <int>, <string>]
英文:

I want get informations in JSON-RPC file with this structure :

{
    &quot;id&quot;: &quot;foo1&quot;,
    &quot;error&quot;: null,
    &quot;result&quot;: [
        {
            &quot;key&quot;: [
                &quot;hello 1&quot;,
                1,
                &quot;world 1&quot;
            ],
            &quot;val&quot;: {
                &quot;type&quot;: &quot;static&quot;
            }
        },
        {
            &quot;key&quot;: [
                &quot;hello 2&quot;,
                1,
                &quot;world 2&quot;
            ],
            &quot;val&quot;: {
                &quot;type&quot;: &quot;static&quot;
            }
        }
    ]
}

This is my parsing function, Key is string table (can't accept int type) :

type JsonRpcRsp struct {
	Id     string          `json:&quot;id&quot;`
	Error  *string         `json:&quot;error&quot;`
	Result json.RawMessage `json:&quot;result&quot;`
}

type JsonRpcEntry_Val struct {
	Type     string          `json:&quot;type&quot;`
}
type JsonRpcEntry struct {
	Key     [3]string          `json:&quot;key&quot;`
	Val     JsonRpcEntry_Val  `json:&quot;val&quot;`
}

jsonResult := JsonRpcRsp{}
json.Unmarshal(data, &amp;jsonResult)
entries := []JsonRpcEntry{}
for _, val := range jsonResult {
	json.Unmarshal(val.Result, &amp;entries)
}

How to parse "key" table ?... problem is there are different types

key table structure is :

[ &lt;string&gt;, &lt;int&gt;, &lt;string&gt;]

答案1

得分: 2

在Go语言中,要解组不同类型的数组,你需要使用接口和类型断言(type assertions),如果你需要访问这些类型。

以下代码可以帮助你实现这一目标:

type Result struct {
	Key [3]interface{} `json:"key"`
	Val struct {
		Type string `json:"type"`
	} `json:"val"`
}

msg := JsonRpcRsp{}
json.Unmarshal(data, &msg)

var result []Result
json.Unmarshal(msg.Result, &result)

for _, v := range result {
	key1 := v.Key[0].(string)
	key2 := v.Key[1].(float64)
	key3 := v.Key[2].(string)

	fmt.Println(key1, key2, key3)
}

在对这三个接口进行类型断言后,你可以根据具体的使用情况进一步处理它们。

英文:

To unmarshal arrays of different types in Go you'll need to use interfaces and consequently type assertions if you need access to the types.

This will work for you:

type Result struct {
	Key [3]interface{} `json:&quot;key&quot;`
	Val struct {
		Type string `json:&quot;type&quot;`
	} `json:&quot;val&quot;`
}

msg := JsonRpcRsp{}
json.Unmarshal(data, &amp;msg)

var result []Result
json.Unmarshal(msg.Result, &amp;result)

for _, v := range result {
	key1 := v.Key[0].(string)
	key2 := v.Key[1].(float64)
	key3 := v.Key[2].(string)

	fmt.Println(key1, key2, key3)
}

After asserting the three interfaces to their types, you can then work with them further, depending on your use case.

huangapple
  • 本文由 发表于 2023年2月9日 19:09:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/75397753.html
匿名

发表评论

匿名网友

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

确定