Go JSON with simplejson

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

Go JSON with simplejson

问题

尝试使用来自"github.com/bitly/go-simplejson"的JSON库。

  1. url := "http://api.stackoverflow.com/1.1/tags?pagesize=100&page=1"
  2. res, err := http.Get(url)
  3. body, err := ioutil.ReadAll(res.Body)
  4. fmt.Printf("%s\n", string(body)) //WORKS
  5. js, err := simplejson.NewJson(body)
  6. total, _ := js.Get("total").String()
  7. fmt.Printf("Total:%s"+total)

但似乎不起作用!?
尝试访问total和tag字段。

英文:

Trying to use the JSON lib from "github.com/bitly/go-simplejson"

  1. url = "http://api.stackoverflow.com/1.1/tags?pagesize=100&page=1"
  2. res, err := http.Get(url)
  3. body, err := ioutil.ReadAll(res.Body)
  4. fmt.Printf("%s\n", string(body)) //WORKS
  5. js, err := simplejson.NewJson(body)
  6. total,_ := js.Get("total").String()
  7. fmt.Printf("Total:%s"+total )

But it seems it doenst work !?
Trying to access the total and tag fields

答案1

得分: 5

你有几个错误:

  1. 如果你检查JSON响应,你会注意到total字段不是字符串,所以在访问该字段时应该使用MustInt()方法,而不是String()
  2. Printf()方法的调用完全错误。你应该传递一个“模板”,然后根据“占位符”的数量传递相应的参数。

顺便说一句,我强烈建议你在每个地方都检查err != nil,这会对你有很大帮助。

这是一个可工作的示例:

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/bitly/go-simplejson"
  5. "io/ioutil"
  6. "log"
  7. "net/http"
  8. )
  9. func main() {
  10. url := "http://api.stackoverflow.com/1.1/tags?pagesize=100&page=1"
  11. res, err := http.Get(url)
  12. if err != nil {
  13. log.Fatalln(err)
  14. }
  15. body, err := ioutil.ReadAll(res.Body)
  16. if err != nil {
  17. log.Fatalln(err)
  18. }
  19. // fmt.Printf("%s\n", string(body))
  20. js, err := simplejson.NewJson(body)
  21. if err != nil {
  22. log.Fatalln(err)
  23. }
  24. total := js.Get("total").MustInt()
  25. if err != nil {
  26. log.Fatalln(err)
  27. }
  28. fmt.Printf("Total:%s", total)
  29. }
英文:

You have a few mistakes:

  1. If you'll check the JSON response you'll notice that total field is not string, that's why you should use MustInt() method, not String(), when you are accessing the field.
  2. Printf() method invocation was totally wrong. You should pass a "template", and then pass arguments appropriate to the number of "placeholders".

By the way, I strongly recomend you to check err != nil everywhere, that'll help you a lot.

Here is the working example:

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/bitly/go-simplejson"
  5. "io/ioutil"
  6. "log"
  7. "net/http"
  8. )
  9. func main() {
  10. url := "http://api.stackoverflow.com/1.1/tags?pagesize=100&page=1"
  11. res, err := http.Get(url)
  12. if err != nil {
  13. log.Fatalln(err)
  14. }
  15. body, err := ioutil.ReadAll(res.Body)
  16. if err != nil {
  17. log.Fatalln(err)
  18. }
  19. // fmt.Printf("%s\n", string(body))
  20. js, err := simplejson.NewJson(body)
  21. if err != nil {
  22. log.Fatalln(err)
  23. }
  24. total := js.Get("total").MustInt()
  25. if err != nil {
  26. log.Fatalln(err)
  27. }
  28. fmt.Printf("Total:%s", total)
  29. }

huangapple
  • 本文由 发表于 2014年1月29日 21:41:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/21432848.html
匿名

发表评论

匿名网友

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

确定