英文:
Go: Filter JSON response
问题
我正在尝试返回一个JSON响应,只过滤掉id大于5的结构值。
可以在这里找到一个示例基本代码:http://play.golang.org/p/4ORba3y7F7
如何过滤JSON结果?
英文:
I'm trying to return a json response that is filtered by only taking the struct values if the id is > 5.
A sample base code can be found here: http://play.golang.org/p/4ORba3y7F7
How do I filter the json results?
答案1
得分: 2
不确定JSON在哪里起作用。
我猜这是你想要的代码:
http://play.golang.org/p/sEkfcEN2DJ
package main
import "fmt"
type Ping struct {
Content []aContent
}
type aContent struct {
Type string
Id int
Created_at int64
}
func main() {
f := Ping{Content: []aContent{{Type: "Hello", Id: 2}, {Type: "World", Id: 6}}}
for i := range f.Content {
if f.Content[i].Id > 5 {
fmt.Println(f.Content[i])
}
}
}
英文:
Not sure where JSON comes into it.
I'm guessing this is what you're after:
http://play.golang.org/p/sEkfcEN2DJ
package main
import "fmt"
type Ping struct {
Content []aContent
}
type aContent struct {
Type string
Id int
Created_at int64
}
func main() {
f := Ping{Content: []aContent{{Type: "Hello", Id: 2}, {Type: "World", Id: 6}}}
for i := range f.Content {
if f.Content[i].Id > 5 {
fmt.Println(f.Content[i])
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论