How to return JSON by specific parameter?

xuanbao · · 440 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>This is my struct:</p> <pre><code>type Route struct { Ip string `json:&#34;ip,omitempty,&#34;` Id int `json:&#34;id,omitempty&#34;` Time time.Time `json:&#34;time,omitempty&#34;` Spackage string `json:&#34;spackage,omitempty&#34;` Smethod string `json:&#34;smethod,omitempty&#34;` Level string `json:&#34;level,omitempty&#34;` Message string `json:&#34;message,omitempty&#34;` Data string `json:&#34;data,omitempty&#34;` } type Routes []Route </code></pre> <p>And this is my route create function:</p> <pre><code>func RouteCreate(w http.ResponseWriter, r *http.Request) { var route Route body, err := ioutil.ReadAll(io.LimitReader(r.Body, 1048576)) if err != nil { panic(err) } if err := r.Body.Close(); err != nil { panic(err) } if err := json.Unmarshal(body, &amp;route); err != nil { w.Header().Set(&#34;Content-Type&#34;, &#34;application/json; charset=UTF-8&#34;) w.WriteHeader(http.StatusUnprocessableEntity) // Errorcode = unprocessable entity. if err := json.NewEncoder(w).Encode(err); err != nil { panic(err) } } t := RepoCreateRoute(route) w.Header().Set(&#34;Content-Type&#34;, &#34;application/json; charset=UTF-8&#34;) w.WriteHeader(http.StatusCreated) if err := json.NewEncoder(w).Encode(t); err != nil { panic(err) } } </code></pre> <p>This is my RepoCreateRoute function:</p> <pre><code>func RepoCreateRoute(t Route) Route { currentId += 1 t.Id = currentId routes = append(routes, t) return t </code></pre> <p>}</p> <p>And this is my RepoFindRoute function:</p> <pre><code>func RepoFindRoute(id int) Route { for _, t := range routes { if t.Id == id { return t } } return Route{} // Will return empty route. </code></pre> <p>}</p> <p>// Now I would like to make a function that sorts the json to show only a specific IP that the user will input. I&#39;ve been struggling with this for a couple of days now and it feels like I can&#39;t solve the issue (I&#39;m quite new to GO and coding as a whole)</p> <hr/>**评论:**<br/><br/>Lord_Peppe: <pre><p>Sorts? Or filters routes to return one or more routes that contain a searched for IP? </p> <p>Make something like your RepoFindRoute, but search by the IP field. If you want to return multiple routes, don&#39;t return when searching the routes array make a array/slice of matching values and return that at the end.</p></pre>JonTargaryanTheFirst: <pre><p>Thank you, i did like you said and now its working (almost). It doesnt show more then one result when searching for a specific ip :/</p></pre>Redundancy_: <pre><p>I&#39;d recommend writing a Go playground example / unit test for this kind of thing, which can help clarify what you mean when you say something like &#34;sorts the json&#34;.</p></pre>JonTargaryanTheFirst: <pre><p>I&#39;ll do that for sure once I get to work on monday :)</p></pre>Lord_Peppe: <pre><p>When you found your matching IP did you do what you did in your ID search and return (ending the loop) or did you add it to an array/slice and then return that slice when the loop finished?</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

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