简体   繁体   English

Json在Golang中解码/解组

[英]Json decode/unmarshal in golang

I have the following json: 我有以下json:

{"results":
[{"columns":["room_id","player_name","player_ip"],
  "types":["integer","text","text"],
  "values":[[1,"alice","127.0.0.1"]
            [1,"bob","127.0.0.1"]],
  "time":0.00018839100000000002}]}

where values can have any number of elements [] inside them. 值中可以包含任意数量的元素[]。 When i try to unmarshal the json into a struct, the "values" tag does not get parsed properly 当我尝试将json解组到结构中时,“ values”标记无法正确解析

struct: 结构:

type queryResults struct {
        Results []struct {
            Columns []string `json:"columns"`
            Types []string `json:"types"`
            Values []struct {
                Room_id int
                Player_name string
                Player_ip string
            } `json:"values"`
            Time float64 `json:"time"`
        } `json:"results"`
    }

Code: 码:

//jsonString is the string input to Unmarshal
resultjson := queryResults{}
json.Unmarshal([]byte(jsonString), &resultjson)
fmt.Printf("%+v",resultjson)

Current Output: 电流输出:

{Results:
 [{Columns:[room_id player_name player_ip] 
   Types:[integer text text] 
   Values:[{room_id:0 player_name: player_ip:} 
           {room_id:0 player_name: player_ip:}] 
   Time:0.00018839100000000002}]}

Expected Output: 预期产量:

{Results:
     [{Columns:[room_id player_name player_ip] 
       Types:[integer text text] 
       Values:[{room_id:1 player_name:alice player_ip:127.0.0.1} 
               {room_id:1 player_name:bob player_ip:127.0.0.1}] 
       Time:0.00018839100000000002}]}

The problem is that your struct definition expects "values" to contain an array of objects, but your actual json contains an array of arrays. 问题是您的结构定义期望“值”包含一个对象数组,但是您的实际json包含一个数组数组。 If you check the result of json.Unmarshal() you'll see it reports an error about this. 如果检查json.Unmarshal()的结果,您将看到它报告有关此错误。 try on golang playground 尝试在golang游乐场

error json: cannot unmarshal array into Go value of type struct { Room_id int; Player_name string; Player_ip string }

As far as i know you can't map this directly into the struct, you'd need to read it into an array then post process it into your final type. 据我所知,您不能直接将此映射到结构中,您需要将其读取到数组中,然后将其后处理为最终类型。 Here's an example that successfully parses the json [the post parsing conversion is left as an exercise for the reader] 这是一个成功解析json的示例 [后解析转换作为练习供读者阅读]

{Results:[{Columns:[room_id player_name player_ip] 
          Types:[integer text text] 
          Values:[[1 alice 127.0.0.1] [1 bob 127.0.0.1]]
          Time:0.00018839100000000002}]}

Json arrays should be unmarshalled into Go slices or arrays. Json数组应解组为Go切片或数组。 Looks like you are trying to unmarshal the arrays inside values to a struct 看起来您正在尝试将values内部的数组解组到struct

"values": [[1,"alice","127.0.0.1"], [1,"bob","127.0.0.1"]]

Above array of arrays should be unmarshalled into a Go slice of slices. 上面的array数组应解组为Go slice of slices。

try, 尝试,

type queryResults struct {
    Results []struct {
        Columns []string `json:"columns"`
        Types   []string `json:"types"`
        Values  [][]interface{} `json:"values"`
        Time float64 `json:"time"`
    } `json:"results"`
}

in Go Playground 围棋场

And don't ignore errors. 并且不要忽略错误。 If you tried, 如果你尝试过

err := json.Unmarshal([]byte(jsonString), &resultjson)
if(err != nil){
    fmt.Println(err)
}

you could have seen the error. 您可能已经看到了错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM