Golang-通过更改键值取消编组JSON [英] Golang - Unmarshall JSON with changing key value

查看:76
本文介绍了Golang-通过更改键值取消编组JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将JSON解组到结构中,但是事实证明这很困难,因为外部JSON密钥发生了变化,而我才在一周前才开始使用.这是我的手动尝试:

I'm trying to unmarshall JSON into a struct, but it's proving difficult because the outer JSON key changes and I only started go a week ago. This is my manual attempt:

import (
    "encoding/json"
    "fmt"
    "strconv"
)

type Device struct {
    localUUID       string
    applicationUUID string
    externalUUID    string
    commit          string
    lastSeen        string
    state           string
    progress        float32
}

func main() {
    devices := make([]*Device, 0, 10)

    b := []byte(`{
        "5417871461137421886": {
            "applicationUUID": "test_applicationUUID",
            "commit": "test_commit",
            "lastSeen": "test_lastSeen",
            "localUUID": "E4:F5:13:8E:F5:43",
            "progress": "3.5",
            "externalUUID": "test_externalUUID",
            "state": "test_state"
        },
        "5632882567440442530": {
            "applicationUUID": "test_applicationUUID",
            "commit": "test_commit",
            "lastSeen": "test_lastSeen",
            "localUUID": "E4:F5:13:8E:F5:42",
            "progress": "3.5",
            "externalUUID": "test_externalUUID",
            "state": "test_state"
        },
        "8912255216147730520": {
            "applicationUUID": "test_applicationUUID",
            "commit": "test_commit",
            "lastSeen": "test_lastSeen",
            "localUUID": "E4:F5:13:8E:F5:41",
            "progress": "3.5",
            "externalUUID": "test_externalUUID",
            "state": "test_state"
        }
    }`)

    var f interface{}
    json.Unmarshal(b, &f)
    outer := f.(map[string]interface{})
    for _, value := range outer {
        inner := value.(map[string]interface{})
        device := &Device{}
        device.localUUID = inner["localUUID"].(string)
        device.applicationUUID = inner["applicationUUID"].(string)
        device.externalUUID = inner["externalUUID"].(string)
        device.commit = inner["commit"].(string)
        device.lastSeen = inner["lastSeen"].(string)
        device.state = inner["state"].(string)
        f, _ := strconv.ParseFloat(inner["progress"].(string), 32)
        device.progress = float32(f)

        devices = append(devices, device)
    }

    for _, device := range devices {
        fmt.Println(device)
    }
}

是否有一种方法可以忽略键并遍历值,从而允许我使用json.Unmarshal(b,& Device)?

Is there a way to ignore the keys and iterate over the values instead, allowing me to use json.Unmarshal(b, &Device)?

推荐答案

您有一系列JSON对象,映射是每个Device的唯一ID.将其解组为map

You have a series of JSON objects, mapping a unique id to each Device. Unmarshal that into a map

type Device struct {
    LocalUUID       string  `json:"localUUID"`
    ApplicationUUID string  `json:"applicationUUID"`
    ExternalUUID    string  `json:"externalUUID"`
    Commit          string  `json:"commit"`
    LastSeen        string  `json:"lastSeen"`
    State           string  `json:"state"`
    Progress        float32 `json:"progress,string"`
}

func main() {
    devices := make(map[string]*Device)

    err := json.Unmarshal(b, &devices)
    if err != nil {
        log.Fatal(err)
    }

    for _, device := range devices {
        fmt.Printf("%#v\n", device)
    }
}

https://play.golang.org/p/JDZzG64jJR

这篇关于Golang-通过更改键值取消编组JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆