![How to Handle Nested JSON Structures and Avoid](https://img.php.cn/upload/article/000/000/000/173249485833449.jpg)
JSON mit verschachtelten Strukturen entmarshaling
Bei der Arbeit mit komplexen JSON-Antworten kann es gelegentlich zu Fehlern wie „String kann nicht im Go-Strukturfeld entmarshaliert werden“ kommen ". Dies tritt normalerweise auf, wenn die JSON-Antwort einen Zeichenfolgenwert enthält, der eine andere JSON-Struktur darstellt.
Berücksichtigen Sie diese unvollständige Go-Struktur ManifestResponse und eine entsprechende JSON-Antwort:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | 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" `
}
|
Nach dem Login kopieren
1 2 3 4 5 6 7 8 9 10 | {
"name" : "library/redis" ,
"tag" : "latest" ,
"architecture" : "amd64" ,
"history" : [
{
"v1Compatibility" : "{\"id\":\"ef8a93741134ad37c834c32836aefbd455ad4aa4d1b6a6402e4186dfc1feeb88\",\"parent\":\"9c8b347e3807201285053a5413109b4235cca7d0b35e7e6b36554995cfd59820\",\"created\":\"2017-10-10T02:53:19.011435683Z\"}"
}
]
}
|
Nach dem Login kopieren
Beim Versuch Wenn Sie den JSON-Code in die Go-Struktur entpacken, tritt möglicherweise der folgende Fehler auf:
1 | json: cannot unmarshal string into Go struct field .v1Compatibility of type struct { ID string "json:\"id\"" ; Parent string "json:\"parent\"" ; Created string "json:\"created\"" }
|
Nach dem Login kopieren
Das Problem liegt an die Tatsache, dass v1Compatibility in der JSON-Antwort ein Zeichenfolgenwert ist, der JSON-Inhalt enthält. Um dies zu beheben, können wir ein Unmarshaling in zwei Durchgängen implementieren:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | 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" `
}
|
Nach dem Login kopieren
Führen Sie dann den folgenden Unmarshaling-Prozess aus:
1 2 3 4 5 6 7 8 9 10 11 12 13 | 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
}
|
Nach dem Login kopieren
Mit diesem Ansatz können Sie verschachtelte JSON-Strukturen mit zwei Schritten verarbeiten -stufiger Unmarshaling-Prozess, der den Fehler „Zeichenfolge kann nicht in Go-Strukturfeld entmarshaliert werden“ verhindert.
Das obige ist der detaillierte Inhalt vonWie gehe ich mit verschachtelten JSON-Strukturen um und vermeide den Fehler „Zeichenfolge kann nicht in Go-Strukturfeld entmarshaliert werden'?. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!