twitter_status_json.go 745 B

12345678910111213141516171819202122232425262728293031323334
  1. // twitter_status_json.go
  2. package main
  3. import (
  4. "encoding/json"
  5. "fmt"
  6. "io/ioutil"
  7. "net/http"
  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. */