亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將 JSON 解組為結構體

將 JSON 解組為結構體

Go
一只甜甜圈 2021-11-22 18:44:46
我的問題很小但非常令人沮喪,因為我似乎無法得到答案。我正在嘗試訪問來自 Google Script 的響應的 JSON 部分。在 Golang 中,我設法將其剝離為這個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"}]]但我需要通過擺脫map[@type:type.googleapis.com/google.apps.script.v1.ExecutionResponse result:[所以我只有結果部分。到目前為止,我已經嘗試將它解組到我的結構中,但沒有成功。這是結構type Case struct {    ID int                CaseNumber string     DateDelivered string     Judge string     Court string     Location string                                       Accused string     Prosecution string     Judgment string}任何幫助將不勝感激。編輯:我所說的解組部分的意思是當我嘗試解組到我的結構中時(即使在修復了結構之后)我得到了錯誤json: cannot unmarshal object into Go value of type []Case這是我開始工作所需的代碼http://play.golang.org/p/rmsvfPVx52。
查看完整描述

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

  }


查看完整回答
反對 回復 2021-11-22
?
至尊寶的傳說

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"

}

]


查看完整回答
反對 回復 2021-11-22
?
守候你守候我

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


查看完整回答
反對 回復 2021-11-22
  • 3 回答
  • 0 關注
  • 202 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號