簡體   English   中英

Golang-通過更改鍵值來解組JSON

[英]Golang - Unmarshall JSON with changing key value

我試圖將JSON解組到結構中,但是事實證明這很困難,因為外部JSON密鑰發生了變化,而我才在一周前才開始使用。 這是我的手動嘗試:

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)?

您有一系列JSON對象, 唯一的ID 映射到每個Device 將其解組到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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM