简体   繁体   English

将JSON解组为struct

[英]Unmarshal JSON into struct

My issue is very small but very frustrating since I can't seem to get the answer. 我的问题很小,但很沮丧,因为我似乎找不到答案。 I am trying to access the JSON part of a response from Google Script. 我正在尝试从Google Script访问响应的JSON部分。 In Golang, I have managed to strip it down to this 在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"
}
]]

but I need to strip it down one level further by getting rid of 但是我需要通过摆脱它来将其进一步降低一级

map[@type:type.googleapis.com/google.apps.script.v1.ExecutionResponse result:[

so I only have the results part. 所以我只有结果部分。

So far I have tried unmarshalling it to my struct with no success. 到目前为止,我尚未尝试将其解组到我的结构中,但没有成功。 Here is the struct 这是结构

type Case struct {
    ID int            
    CaseNumber string 
    DateDelivered string 
    Judge string 
    Court string 
    Location string                                   
    Accused string 
    Prosecution string 
    Judgment string
}

Any help will be highly appreciated. 任何帮助将不胜感激。

EDIT: What I meant by the unmarshaling part is that when I try unmarshaling into my struct (even after fixing the struct) I get the error 编辑:解组部分的意思是,当我尝试对结构进行解组(即使修复了结构)后,也会收到错误消息

json: cannot unmarshal object into Go value of type []Case

This is the code I need to get to work http://play.golang.org/p/rmsvfPVx52 . 这是我需要开始工作的代码http://play.golang.org/p/rmsvf​​PVx52

You need to export the fields in Case by starting the name with an uppercase character. 您需要通过以大写字母开头名称来导出 Case中的字段。

type Case struct {
  ID int            
  CaseNumber string 
  DateDelivered string 
  Judge string 
  Court string 
  Location string                                   
  Accused string 
  Prosecution string 
  Judgment string
}

The encoding/json package and similar packages ignore unexported fields. encoding / json包和类似的包会忽略未导出的字段。

Use a slice to decode a JSON array: 使用切片来解码JSON数组:

  var result []Case
  err := json.Unmarshal(data, &result)
  if err != nil {
     // handle error
  }

Playground Example 操场上的例子

Where c is 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"
}
]]

I did; 我做到了

case:= c.(map[string]interface {})
fmt.Println(case["result"])

which gives; 这使;

[
{
    "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"
}
]

As @CodingPickle indicated above, you need to strip the non-valid json first: 如上面的@CodingPickle所示,您需要先去除无效的json:

data := `{"result":[
{
    "id": 1,
    ...
},
{
    "id": 2,
    ...
}]}`

As well, you need to add the json defn's to the struct: 同样,您需要将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"`
}

Example: 例:

http://play.golang.org/p/KUbDpSxMVI http://play.golang.org/p/KUbDpSxMVI

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM