json.go 1014 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package main
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. )
  7. type tweet struct {
  8. topicPointer string
  9. textPointer int
  10. Text string
  11. Topics []string
  12. }
  13. var topicList []string
  14. func test() {
  15. topicList = append(topicList, "Sport", "Ball", "Kick")
  16. fmt.Println(topicList[0])
  17. topicByteArray := new(bytes.Buffer)
  18. json.NewEncoder(topicByteArray).Encode(topicList)
  19. fmt.Println(topicByteArray.String())
  20. var recBookmark []string
  21. arrayReader := bytes.NewReader(topicByteArray.Bytes())
  22. json.NewDecoder(arrayReader).Decode(&recBookmark)
  23. fmt.Println(recBookmark)
  24. var tweets []tweet
  25. tweet1 := tweet{"", 0, "Let's go", topicList}
  26. tweet2 := tweet{"", 0, "HeyHo", topicList}
  27. tweets = append(tweets, tweet1, tweet2)
  28. tweetByteArray := new(bytes.Buffer)
  29. json.NewEncoder(tweetByteArray).Encode(tweets)
  30. fmt.Println(tweetByteArray.String())
  31. var recTweets []tweet
  32. tweetReader := bytes.NewReader(tweetByteArray.Bytes())
  33. json.NewDecoder(tweetReader).Decode(&recTweets)
  34. fmt.Println(recTweets[0].Text)
  35. }