2015-09-23 183 views
-2

我想解析提及的json解析嵌套json對象golang

{ 「foo」 的: [{ 「欄」:1, 「巴茲」:2},{ 「酒吧」:4 「巴茲」:25}], 「更多」: 「文本」 }

使用以下結構來解組

type FooStruct struct { 
    Bar int `json:"bar"` 
    Baz int `json:"baz"` 
} 

type ResponseStruct struct { 
    More string `json:"more"` 
    Foo []FooStruct `json:"foo"` 
} 
var contentHtml ResponseStruct 
err = json.Unmarshal(<byte_array>, &contentHtml) 
fmt.Printf("%+v", contentHtml.FooStruct[0].Bar) 
+1

問題是什麼? https://play.golang.org/p/-B3jNUqQHF – RoninDev

回答

0

您的代碼是corrent,除了這一行:

fmt.Printf("%+v", contentHtml.FooStruct[0].Bar) 

ResponseStruct類型中沒有字段FooStruct。我認爲你需要使用Foo
關於的工作示例Go playground

+0

謝謝!這是工作 – DakzBack