New to Go, trying to sort out how to process JSON

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

New to Go, trying to sort out how to process JSON

问题

从Python、Ruby和JS等语言过来,我现在真的在使用Go时遇到了很多困难。它感觉过于复杂,但我希望我只是漏掉了什么。

目前,我有一段代码可以成功调用波士顿的MBTA API(使用他们的公共开发者密钥)并返回所有路线信息。

我已经在这里放置了代码:http://pastebin.com/PkBaP714 和 http://pastebin.com/7mRxgrpp

返回的示例数据:http://pastebin.com/M2hzMKYs

我想要返回两件事情:1)仅仅是每个route_type和mode_name,以及2)当调用route_type时,每个route_id和route_name。

出于某种原因,我完全迷失了。我已经花了16个小时盯着文档,感觉就像在看外语:)。

也许要求具体的帮助有点过分,但我会非常感激的。

英文:

Coming from languages like Python, Ruby, and JS, I am really struggling with Go right now. It feels overly complex, but I am hoping I am just missing something.

Right now I have code that can successfully call Boston's MBTA API (using their public developer key) and return all route information.

I have dropped the code here: http://pastebin.com/PkBaP714 and here: http://pastebin.com/7mRxgrpp

Sample data returned: http://pastebin.com/M2hzMKYs

I want to return two things 1) JUST each route_type and mode_name, and 2) when route_type is called each of the route_id and route_name.

For whatever reason I am just totally lost. I've spent 16 hours staring at documentation and I feel like I am looking at a foreign language :).

It may be too much to ask for specific help, but I would LOVE IT.

答案1

得分: 1

只需将它们映射到一个新类型:

func main() {
    flag.Parse()
    c := gombta.Client{APIKey: apikey, URL: apiurl}

    // 按类型获取路由列表
    d, err := c.GetRoutes(format)
    check(err)

    var toPrint interface{}

    if typeid == 9999 {
        type Result struct {
            RouteType string `json:"route_type"`
            ModeName  string `json:"mode_name"`
        }
        rs := []Result{}
        for _, m := range d.Mode {
            rs = append(rs, Result{
                RouteType: m.RouteType,
                ModeName:  m.ModeName,
            })
        }
        toPrint = rs
    } else {
        type Result struct {
            RouteID   string `json:"route_id"`
            RouteName string `json:"route_name"`
        }
        rs := []Result{}
        for _, m := range d.Mode {
            if fmt.Sprint(typeid) == m.RouteType {
                for _, r := range m.Route {
                    rs = append(rs, Result{
                        RouteID:   r.RouteID,
                        RouteName: r.RouteName,
                    })
                }
            }
        }
        toPrint = rs
    }

    j, err := json.MarshalIndent(toPrint, "", " ")
    fmt.Printf("RouteTypes: ")
    os.Stdout.Write(j)
}
英文:

Just map them to a new type:

func main() {
	flag.Parse()
	c := gombta.Client{APIKey: apikey, URL: apiurl}

	// get a list of routes by type
	d, err := c.GetRoutes(format)
	check(err)

	var toPrint interface{}

	if typeid == 9999 {
		type Result struct {
			RouteType string `json:"route_type"`
			ModeName  string `json:"mode_name"`
		}
		rs := []Result{}
		for _, m := range d.Mode {
			rs = append(rs, Result{
				RouteType: m.RouteType,
				ModeName:  m.ModeName,
			})
		}
		toPrint = rs
	} else {
		type Result struct {
			RouteID   string `json:"route_id"`
			RouteName string `json:"route_name"`
		}
		rs := []Result{}
		for _, m := range d.Mode {
			if fmt.Sprint(typeid) == m.RouteType {
				for _, r := range m.Route {
					rs = append(rs, Result{
						RouteID:   r.RouteID,
						RouteName: r.RouteName,
					})
				}
			}
		}
		toPrint = rs
	}

	j, err := json.MarshalIndent(toPrint, "", " ")
	fmt.Printf("RouteTypes: ")
	os.Stdout.Write(j)
}

huangapple
  • 本文由 发表于 2015年9月14日 05:18:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/32554642.html
匿名

发表评论

匿名网友

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

确定