无法将字符串解组到Go struct字段中 [英] cannot unmarshal string into Go struct field

查看:423
本文介绍了无法将字符串解组到Go struct字段中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有以下(不完整的类型)(这是来自清单端点v2模式1的docker API的响应

I have the following (incomplete type) (which is a response from docker API from manifests endpoint v2 schema 1 https://docs.docker.com/registry/spec/manifest-v2-1/)

type ManifestResponse struct {
    Name         string `json:"name"`
    Tag          string `json:"tag"`
    Architecture string `json:"architecture"`

    FsLayers []struct {
        BlobSum string `json:"blobSum"`
    } `json:"fsLayers"`

    History []struct {
        V1Compatibility struct {
            ID              string `json:"id"`
            Parent          string `json:"parent"`
            Created         string `json:"created"`
        } `json:"v1Compatibility"`
    } `json:"history"`
}

得到以下响应时:

 { "schemaVersion": 1,
   "name": "library/redis",
   "tag": "latest",
   "architecture": "amd64",
   "history": [
      {
         "v1Compatibility": "{\"id\":\"ef8a93741134ad37c834c32836aefbd455ad4aa4d1b6a6402e4186dfc1feeb88\",\"parent\":\"9c8b347e3807201285053a5413109b4235cca7d0b35e7e6b36554995cfd59820\",\"created\":\"2017-10-10T02:53:19.011435683Z\"}"
      }
   ]
}

在尝试使用以下代码片段对其进行反序列化时:

While trying to deserialize it using the following snippet of code:

var jsonManResp ManifestResponse                                                                                                                                                                                                                                                
if err = json.NewDecoder(res.Body).Decode(&jsonManResp); err != nil {
    log.Fatal(err)
}

我收到以下错误:

json: cannot unmarshal string into Go struct field .v1Compatibility of type struct { ID string "json:\"id\""; Parent string "json:\"parent\""; Created string "json:\"created\"" }

完整的游乐场代码: https://play.golang.org/p/tHzf9GphWX

可能是什么问题?

推荐答案

问题是v1Compatibility是JSON中的字符串值;该值恰好是JSON内容,但是它在JSON字符串中,因此不能一步一步将其解组.您可以分两步将其解组:

The problem is that v1Compatibility is a string value in the JSON; the value happens to be JSON content, but it's inside a JSON string, so can't be unmarshalled all in one step. You could instead unmarshal it in two passes:

type ManifestResponse struct {
    Name         string `json:"name"`
    Tag          string `json:"tag"`
    Architecture string `json:"architecture"`

    FsLayers []struct {
        BlobSum string `json:"blobSum"`
    } `json:"fsLayers"`

    History []struct {
        V1CompatibilityRaw string `json:"v1Compatibility"`
        V1Compatibility V1Compatibility
    } `json:"history"`
}

type V1Compatibility struct {
    ID              string `json:"id"`
    Parent          string `json:"parent"`
    Created         string `json:"created"`
}

然后:

var jsonManResp ManifestResponse
if err := json.Unmarshal([]byte(exemplar), &jsonManResp); err != nil {
    log.Fatal(err)
}
for i := range jsonManResp.History {
    var comp V1Compatibility
    if err := json.Unmarshal([]byte(jsonManResp.History[i].V1CompatibilityRaw), &comp); err != nil {
        log.Fatal(err)
    }
    jsonManResp.History[i].V1Compatibility = comp
}

此处是工作场所的示例: https://play.golang.org/p/QNsu5_63E0

Working playground example here: https://play.golang.org/p/QNsu5_63E0

这篇关于无法将字符串解组到Go struct字段中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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