小编Gir*_*ana的帖子

在golang中解析没有索引名称的json

我有一个以下格式的json

{
    "status_code": 200,
    "status_message": "OK",
    "response": {
        "Messages": [
            "CODE_NOT_AVAILABLE"
        ],
        "UnknownDevices": {
            "": [
                "6",
                "7",
                "8",
                "9",
                "10"
            ]
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

正如我们所看到的那样,我们缺少一个索引键,在unknownDevices之后,我试图用以下方式使用golan解组这个json

package main

import (
    "encoding/json"
    "fmt"
)

type pushWooshResponse struct {
    Status     int    `json:"status_code"`
    Status_msg string `json:"status_message"`
    Response   response
}

type response struct {
    Message        []string `json:"Messages"`
    UnknownDevices devices
}

type devices struct {
    Udevices []string `json:""`
}

func main() {
    itemInfoR := `{"status_code":200,"status_message":"OK","response":{"Messages":["CODE_NOT_AVAILABLE"],"UnknownDevices":{"devices":["6","7","8","9","10"]}}}`
    itemInfoBytes := []byte(itemInfoR)
    var ItemInfo pushWooshResponse
    er := json.Unmarshal(itemInfoBytes, &ItemInfo) …
Run Code Online (Sandbox Code Playgroud)

json go

3
推荐指数
1
解决办法
1068
查看次数

标签 统计

go ×1

json ×1