将 JSON 反序列化为嵌套结构体。

huangapple go评论98阅读模式
英文:

Unmarshmaling json to nested struct

问题

我有不同的 JSON 字节输入,需要将其解组为嵌套的 JSON 结构。我能够将 JSON 解组为 App 结构体。然而,我无法添加到 "status" 结构体中。

我尝试解组,但由于 app1 和 app2 的类型是 App 而不是字节,所以无法工作。而且尝试直接设置会出现错误 "cannot use app1 (type App) as type []App in assignment"。

以下是翻译好的代码:

package main

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

type App struct {
	Appname string `json:"appname"`
	Builds  string `json:"builds"`
}

type Status struct {
	Apps []App `json:"apps"`
}

func main() {
	jsonData1 := []byte(`
			{  
			   "appname": "php1",
			   "buildconfigs":"deleted"
			}
		`)

	jsonData2 := []byte(`
			{  
			   "appname": "php2",
			   "buildconfigs":"exists"
			}
		`)

	// 解组 JSON 数据到 App
	var app1 App
	json.Unmarshal(jsonData1, &app1)

	var app2 App
	json.Unmarshal(jsonData2, &app2)

	var status Status
	//json.Unmarshal(app1, &status)
	//json.Unmarshal(app2, &status)

	status.Apps = append(status.Apps, app1)
	status.Apps = append(status.Apps, app2)

	fmt.Println(reflect.TypeOf(app1))
	fmt.Println(reflect.TypeOf(app2))
}

希望对你有帮助!

英文:

I have different json byte inputs that I need to unmarshal into a nested json structure. I am able to unmarshal the json into the struct App. However I am unable to add to "status" struct.

I tried to unmarshal, but that doesnt work since my app1 & app2 are of type App instead of bytes. And trying to set directly gives the error "cannot use app1 (type App) as type []App in assignment"

package main

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

type App struct {
	Appname string `json:"appname"`
	Builds  string `json:"builds"`
}

type Status struct {
	Apps []App `json:"apps"`
}

func main() {
	jsonData1 := []byte(`
			{  
			   "appname": "php1",
			   "buildconfigs":"deleted"
			}
		`)

	jsonData2 := []byte(`
			{  
			   "appname": "php2",
			   "buildconfigs":"exists"
			}
		`)

	// unmarshal json data to App
	var app1 App
	json.Unmarshal(jsonData1, &app1)

	var app2 App
	json.Unmarshal(jsonData2, &app2)

	var status Status
	//json.Unmarshal(app1, &status)
	//json.Unmarshal(app2, &status)

	status.Apps = app1
	status.Apps = app2

	fmt.Println(reflect.TypeOf(app1))
	fmt.Println(reflect.TypeOf(app1))
}

答案1

得分: 1

你不能将单个元素分配给数组字段,所以将你的代码修改为以下形式:

status.Apps = []App{app1, app2}

或者

status.Apps = []App{}
status.Apps = append(status.Apps, app1)
status.Apps = append(status.Apps, app2)

此外,你的 JSON 字段名为 buildconfigs,而在结构体定义中为 json:"builds"。在这种情况下,结构体字段将始终为空。

工作示例:http://play.golang.org/p/fQ-XQsgK3j

英文:

You can't assign single element to array field so convert your

status.Apps = app1
status.Apps = app2

to something like

status.Apps = []App{app1, app2}

or

status.Apps = []App{}
status.Apps = append(status.Apps, app1)
status.Apps = append(status.Apps, app2)

Also your JSON field named buildconfigs and in struct specification json:"builds". Structure's field always will be empty in this case.

Working example http://play.golang.org/p/fQ-XQsgK3j

答案2

得分: 1

你的问题对我来说有点困惑:s 但是如果你将你的JSON数据修改为JSON数组,它将可以与Unmarshal一起使用,并且可以无问题地解组为状态:

func main() {
    jsonData1 := []byte(`
        {  
           "apps": [{
               "appname": "php1",
               "buildconfigs":"deleted"
            },{  
               "appname": "php2",
               "buildconfigs":"exists"
            }]
        }
    `)

    var status Status
    json.Unmarshal(jsonData1, &status)
    fmt.Printf("%+v\n", status)
}

工作示例在这里:http://play.golang.org/p/S4hOxJ6gHz

英文:

Your question is a bit confusing to me :s But if you modify your JSON data to an JSON array and it will work with Unmarshal and could be unmarshal to status with no problems:

func main() {
    jsonData1 := []byte(`
            {  
               "apps": [{
                   "appname": "php1",
                   "buildconfigs":"deleted"
                },{  
                   "appname": "php2",
                   "buildconfigs":"exists"
                }]
            }
        `)

    var status Status
    json.Unmarshal(jsonData1, &status)
    fmt.Printf("%+v\n", status)
}

Working example here: http://play.golang.org/p/S4hOxJ6gHz

huangapple
  • 本文由 发表于 2016年4月1日 15:43:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/36350730.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定