struct of nested json from api

agolangf · · 450 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Hello, I have issues wich I don&#39;t understand, I started goLang 3 days ago and can&#39;t sucess to get my api-rest response in Json cause it&#39;s a <strong>nested one</strong></p> <p>let me show you</p> <p>this is my response in String</p> <hr/> <pre><code>{ &#34;numResults&#34;: 1, &#34;results&#34;: [ { &#34;nodeRef&#34;: &#34;workspace://SpacesStore/0e263e8d-b5de-4e6f-9835-e1dbef108620&#34;, &#34;qnamePath&#34;: { &#34;name&#34;: &#34;/{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&#34;, &#34;prefixedName&#34;: &#34;/app:company_home/st:sites/cm:gestion-courrier/cm:documentLibrary/cm:Contrat_x0020_DAV/cm:_x0031_03201707210005476501.pdf&#34; }, &#34;name&#34;: { &#34;name&#34;: &#34;{http://www.alfresco.org/model/content/1.0}103201707210005476501.pdf&#34;, &#34;prefixedName&#34;: &#34;cm:103201707210005476501.pdf&#34; }, &#34;parentNodeRef&#34;: &#34;&#34; } ], &#34;searchElapsedTime&#34;: 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:&#34;name&#34;` prefixedName string `json:&#34;prefixedName&#34;` } type nameStruct struct { name string `json:&#34;name&#34;` prefixedName string `json:&#34;prefixedName&#34;` } type findNodeStructResults struct { nodeRef string `json:&#34;nodeRef&#34;` qnamePathStruct nameStruct parentNodeRef string `json:&#34;parentNodeRef&#34;` } type findNodeStruct struct { numResults int64 `json:&#34;numResults&#34;` results []findNodeStructResults `json:&#34;results&#34;` searchElapsedTime int64 `json:&#34;searchElapsedTime&#34;` } </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, &amp;s) //create an IOReader interface for newDecoder technique // r := bytes.NewReader(body) // err := json.NewDecoder(r).Decode(&amp;s) if(err != nil){ fmt.Println(&#34;whoops:&#34;, 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&#39;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

450 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传