twitter_status_json.go 883 B

123456789101112131415161718192021222324252627282930313233
  1. // twitter_status_json.go
  2. package main
  3. import (
  4. "net/http"
  5. "fmt"
  6. "encoding/json"
  7. "io/ioutil"
  8. )
  9. type Status struct {
  10. Text string
  11. }
  12. type User struct {
  13. Status Status
  14. }
  15. func main() {
  16. /* perform an HTTP request for the twitter status of user: Googland */
  17. res, _:= http.Get("http://twitter.com/users/Googland.json")
  18. /* initialize the structure of the JSON response */
  19. user := User{Status{""}}
  20. /* unmarshal the JSON into our structures */
  21. temp, _ := ioutil.ReadAll(res.Body)
  22. body := []byte(temp)
  23. json.Unmarshal(body, &user)
  24. fmt.Printf("status: %s", user.Status.Text)
  25. }
  26. /* Output:
  27. status: Robot cars invade California, on orders from Google:
  28. Google has been testing self-driving cars ... http://bit.ly/cbtpUN http://retwt.me/97p
  29. */