Go:过滤 JSON 响应

huangapple go评论77阅读模式
英文:

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])
		}
    }
}

huangapple
  • 本文由 发表于 2014年9月25日 00:00:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/26021474.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定