<p>Hello,
I have issues wich I don't understand, I started goLang 3 days ago and can't sucess to get my api-rest response in Json cause it's a <strong>nested one</strong></p>
<p>let me show you</p>
<p>this is my response in String</p>
<hr/>
<pre><code>{
"numResults": 1,
"results": [
{
"nodeRef": "workspace://SpacesStore/0e263e8d-b5de-4e6f-9835-e1dbef108620",
"qnamePath": {
"name": "/{http://www.alfresco.org/model/application/1.0}company_home/{http://www.alfresco.org/model/site/1.0}sites/{http://www.alfresco.org/model/content/1.0}gestion-courrier/{http://www.alfresco.org/model/content/1.0}documentLibrary/{http://www.alfresco.org/model/content/1.0}Contrat_x0020_DAV/{http://www.alfresco.org/model/content/1.0}_x0031_03201707210005476501.pdf",
"prefixedName": "/app:company_home/st:sites/cm:gestion-courrier/cm:documentLibrary/cm:Contrat_x0020_DAV/cm:_x0031_03201707210005476501.pdf"
},
"name": {
"name": "{http://www.alfresco.org/model/content/1.0}103201707210005476501.pdf",
"prefixedName": "cm:103201707210005476501.pdf"
},
"parentNodeRef": ""
}
],
"searchElapsedTime": 16
}
</code></pre>
<hr/>
<p>So I created <strong>my struct based on examples</strong> found on internet (I tested a lot of differents struct)</p>
<hr/>
<pre><code>type qnamePathStruct struct {
name string `json:"name"`
prefixedName string `json:"prefixedName"`
}
type nameStruct struct {
name string `json:"name"`
prefixedName string `json:"prefixedName"`
}
type findNodeStructResults struct {
nodeRef string `json:"nodeRef"`
qnamePathStruct
nameStruct
parentNodeRef string `json:"parentNodeRef"`
}
type findNodeStruct struct {
numResults int64 `json:"numResults"`
results []findNodeStructResults `json:"results"`
searchElapsedTime int64 `json:"searchElapsedTime"`
}
</code></pre>
<hr/>
<p>But I only have <strong>unexpected end of JSON input</strong> in marshall technique, and <strong>EOF error</strong> in NewDecoder.Decode technique</p>
<p>this is my method</p>
<hr/>
<pre><code>func ParseResp(body []byte) (*findNodeStruct, error) {
//declare my structure
var s = new(findNodeStruct)
//try to decode in marshal
err := json.Unmarshal(body, &s)
//create an IOReader interface for newDecoder technique
// r := bytes.NewReader(body)
// err := json.NewDecoder(r).Decode(&s)
if(err != nil){
fmt.Println("whoops:", err)
}
</code></pre>
<hr/>
<p>Thank you in advance</p>
<hr/>**评论:**<br/><br/>shovelpost: <pre><p>This is a complete program that decodes your JSON to a struct: <a href="https://play.golang.org/p/wMb9qY_t9t" rel="nofollow">https://play.golang.org/p/wMb9qY_t9t</a></p>
<p>You might want to tidy up that struct because I generated it quickly from: <a href="https://mholt.github.io/json-to-go/" rel="nofollow">https://mholt.github.io/json-to-go/</a></p></pre>RedRaccoon27: <pre><p>fantastic ! </p>
<p>your auto generated is wonderfull, </p>
<p>thanks :)</p></pre>shovelpost: <pre><blockquote>
<p>your auto generated is wonderful</p>
</blockquote>
<p>It's not mine. You might want to thank <a href="https://twitter.com/mholt6" rel="nofollow">Matt Holt</a> who <a href="https://github.com/mholt/json-to-go" rel="nofollow">made it</a>. </p>
<p>But it is wonderful I agree. :)</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传