Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将JSON解组到结构中_Json_Struct_Go_Google Apps Script - Fatal编程技术网

将JSON解组到结构中

将JSON解组到结构中,json,struct,go,google-apps-script,Json,Struct,Go,Google Apps Script,我的问题很小,但很令人沮丧,因为我似乎无法得到答案。我正在尝试访问Google脚本响应的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:0

我的问题很小,但很令人沮丧,因为我似乎无法得到答案。我正在尝试访问Google脚本响应的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
}
我们将非常感谢您的帮助

EDIT:我所说的解组部分的意思是,当我尝试解组到我的结构中时(即使在修复了结构之后),我得到了错误

json: cannot unmarshal object into Go value of type []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
  }

其中
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"
}
]

正如上面提到的@CodingPickle,您需要首先剥离无效的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"`
}
例如:


当您说您尝试取消编组但未成功时,可以提供更多详细信息。你也可以展示你的代码,这将有助于理解你在尝试什么。到目前为止,我看到除ID之外的Case struct所有其他字段都没有公开,它们不是以大写字母开头的,并且在解组时它们不会被填充。非常感谢!我没有注意到。请检查问题的编辑。我用它来回答你的问题:)非常感谢你的回答。如果我能找到一种方法来剥离
map[@type:type.googleapis.com/google.apps.script.v1.ExecutionResponse结果:[
自动使用代码,而不是手动,我打赌这个解决方案会起作用。不过我可能错了:(。但是请看一下这个文本
映射[@type:type.googleapis.com/google.apps.script.v1.ExecutionResponse
不是有效的JSON。如果服务器返回无效的JSON,您需要手动删除。谢谢。我希望有一个自动化的解决方案。