3 回答
TA贡献1827条经验 获得超9个赞
您需要通过以大写字符开头的名称来导出Case 中的字段。
type Case struct {
ID int
CaseNumber string
DateDelivered string
Judge string
Court string
Location string
Accused string
Prosecution string
Judgment string
}
encoding/json 包和类似的包会忽略未导出的字段。
使用切片解码 JSON 数组:
var result []Case
err := json.Unmarshal(data, &result)
if err != nil {
// handle error
}
TA贡献1789条经验 获得超10个赞
哪里c是
map[@type:type.googleapis.com/google.apps.script.v1.ExecutionResponse result:[
{
"id": 1,
"casenumber": "Criminal Case 20 of 2012",
"datedelivered": "2015-10-22T21:00:00.000Z",
"judge": "George Matatia Abaleka Dulu",
"court": "High Court",
"location": "Garissa",
"accused": "Abdi Sheikh Mohamed",
"judgment": "The accused Abdi Sheikh Mohamed stands charged with the offence of murder contrary to Section 203 as read with Section 204 of the Penal Code. The particulars of the offence are that on 8th May 2012 at Ifo Refugee camp, Lagdera District within Garissa County murdered Othon Ubang Alwal. He has denied the charge."
},
{
"id": 2,
"casenumber": "Criminal Case 21 of 2012",
"datedelivered": "2015-11-22T21:00:00.000Z",
"judge": "Lilo",
"court": "High Court",
"location": "Nairobi",
"accused": "Stitch",
"prosecution": "Milo",
"judgment": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum"
}
]]
我做到了;
case:= c.(map[string]interface {})
fmt.Println(case["result"])
这使;
[
{
"id": 1,
"casenumber": "Criminal Case 20 of 2012",
"datedelivered": "2015-10-22T21:00:00.000Z",
"judge": "George Matatia Abaleka Dulu",
"court": "High Court",
"location": "Garissa",
"accused": "Abdi Sheikh Mohamed",
"judgment": "The accused Abdi Sheikh Mohamed stands charged with the offence of murder contrary to Section 203 as read with Section 204 of the Penal Code. The particulars of the offence are that on 8th May 2012 at Ifo Refugee camp, Lagdera District within Garissa County murdered Othon Ubang Alwal. He has denied the charge."
},
{
"id": 2,
"casenumber": "Criminal Case 21 of 2012",
"datedelivered": "2015-11-22T21:00:00.000Z",
"judge": "Lilo",
"court": "High Court",
"location": "Nairobi",
"accused": "Stitch",
"prosecution": "Milo",
"judgment": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum"
}
]
TA贡献1802条经验 获得超10个赞
您需要先去除无效的 json:
data := `{"result":[
{
"id": 1,
...
},
{
"id": 2,
...
}]}`
同样,您需要将 json defn 添加到结构中:
type Result struct {
Result []Case `json:"result"`
}
type Case struct {
ID int `json:"id"`
CaseNumber string `json:"casenumber"`
DateDelivered string `json:"datedelivered"`
Judge string `json:"judge"`
Court string `json:"court"`
Location string `json:"location"`
Accused string `json:"accused"`
Prosecution string `json:"prosecution"`
Judgment string `json:"judgement"`
}
例子:
http://play.golang.org/p/KUbDpSxMVI
- 3 回答
- 0 关注
- 154 浏览
添加回答
举报