dongou6632 2016-08-05 16:57
浏览 90
已采纳

Golang-通过更改键值来解组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)
    }
}

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

  • 写回答

1条回答 默认 最新

  • dongshi7350 2016-08-05 17:03
    关注

    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
    ", device)
        }
    }
    

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分