我正在學習Go通過編寫一個簡單的http服務器,我需要處理一些JSON響應。 structResult:解組頂級JSON數組Go
與對象響應,我可以用2行代碼地道解組它=美孚{} json.Unmarshal(structBody,& structResult)
我不知道怎麼做相同的數組響應(請參閱下面的示例)。有沒有一種方法來指定(可能通過json標籤)頂層數組應該進入給定的結構域?
package main
import "fmt"
import "encoding/json"
type Foo struct {
Id uint64 `json:"id"`
Name string `json:"name"`
}
type BaseResult struct {
Error string `json:"error"`
}
type FooResult struct {
BaseResult
Foos []Foo
}
func main() {
// Simple and works.
structBody := []byte(`{"id": 1,"name": "foo"}`)
structResult := Foo{}
json.Unmarshal(structBody, &structResult)
fmt.Printf("%#v\n", structResult)
// Doesn't work.
arrayBody := []byte(`[{"id": 1,"name": "foo"},{"id": 2,"name": "bar"},{"id": 3,"name": "foobar"}]`)
arrayResult := FooResult{}
json.Unmarshal(arrayBody, &arrayResult)
fmt.Printf("%#v\n", arrayResult)
}
我知道我可以做FooResult數組:
type FooResult []Foo
但後來我失去了指定基本對象,我想用存儲錯誤消息,並且這樣的能力。我也知道我可以直接解組成& fooResult.Foos,但我希望代碼能夠同時處理對象和數組。
UPDATE
實施UnmarshalJSON通過@dyoo的建議部分地解決我的問題,但我希望我可以用BaseResult存儲解析錯誤的情況下,JSON具有不同的結構:
arrayBody := []byte(`{"error": "foo"}`)
arrayResult := FooResult{}
json.Unmarshal(arrayBody, &arrayResult)
fmt.Printf("%#v\n", arrayResult)
中當然我可以在UnmarshalJSON中實現更復雜的邏輯 - 但是沒有更簡單的方法來實現它嗎?
建議:重命名'FooResult'到'ArrayResult'。使用'Foo'作爲名字的任何部分往往是可疑的,因爲它並不意味着什麼。 – dyoo 2014-09-25 18:16:40
當然,在這個例子中我只使用了名字「foo」。 – proxi 2014-09-26 08:55:14