我正在自学如何在 Golang 中使用 JSON 包。很多事情看起来很简单,但是我在解析从 3D 打印机中检索到的一些 JSON 数据时遇到了麻烦。JSON 如下所示:{ "tasks": [ { "class": "Task", "id": "5fee231a", "instances": { "28253266": { "class": "StateInstance", "id": "28253266", "progress": 1, "stateType": "Y-EdgeAvoiding" }, "1d774b49": { "class": "StateInstance", "id": "1d774b49", "progress": 1, "stateType": "X-Calibration" }, }, "stateType": "StartingUp" } ]}(注意:还有一些“实例”,但为了简洁起见我没有包括它们,但它们都遵循这种模式,但具有不同的 stateType)基本上,打印机有它正在执行的任务(在上面的示例中,该任务的 ID 为5fee231a)以及在其中的子任务(例如 Task 28253266)。如果我使用此代码: var vals interface{} err = json.Unmarshal([]byte(myJSON), &vals) if err != nil { fmt.Println("Error:", err) } spew.Dump(&vals)(github.com/davecgh/go-spew用于转储变量),然后我得到一些输出(注意:这不是整个输出,为了简洁起见,它被剪掉了:)):(*map[string]interface {})(0xc0820068e0)((len=1) { (string) (len=5) "tasks": ([]interface {}) (len=1 cap=1) { (map[string]interface {}) (len=4) { (string) (len=5) "class": (string) (len=4) "Task", (string) (len=2) "id": (string) (len=8) "5fee231a", (string) (len=9) "instances": (map[string]interface {}) (len=13) { (string) (len=8) "bd65d028": (map[string]interface {}) (len=4) { (string) (len=5) "class": (string) (len=13) "StateInstance", (string) (len=2) "id": (string) (len=8) "bd65d028", (string) (len=8) "progress": (float64) 1, (string) (len=9) "stateType": (string) (len=17) "CenteringPosition" }, (string) (len=8) "d1e225e7": (map[string]interface {}) (len=4) { (string) (len=2) "id": (string) (len=8) "d1e225e7", (string) (len=8) "progress": (float64) 1, (string) (len=9) "stateType": (string) (len=10) "TargetCold", (string) (len=5) "class": (string) (len=13) "StateInstance" },
- 2 回答
- 0 关注
- 160 浏览
添加回答
举报
0/150
提交
取消