在Golang中解析JSON文件。

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

Parsing json file in golang

问题

我有一个像这样的 JSON 结构:

  1. {
  2. "some text":[
  3. {
  4. "sha":"1234567",
  5. "message":"hello world",
  6. "author":"varung",
  7. "timestamp":1479445228
  8. }
  9. ]
  10. }

我需要使用 Golang 访问 sha 的值。我该如何做?

英文:

I have a json structure like this

  1. {
  2. "some text":[
  3. {
  4. "sha":"1234567",
  5. "message":"hello world",
  6. "author":"varung",
  7. "timestamp":1479445228
  8. }
  9. ]
  10. }

I need to access the value of sha, using golang. How can I do it?

答案1

得分: 2

你可以很容易地使用Go语言的encoding/json包。以下是下面代码的playground链接

  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "log"
  6. )
  7. var a = `{
  8. "sometext": [
  9. {
  10. "sha": "1234567",
  11. "message": "hello world",
  12. "author": "varung",
  13. "timestamp": 1479445228
  14. }
  15. ]
  16. }`
  17. type Root struct {
  18. Text []*Object `json:"sometext"`
  19. }
  20. type Object struct {
  21. Sha string `json:"sha"`
  22. Message string `json:"message"`
  23. Author string `json:"author"`
  24. Timestamp int `json:"timestamp"`
  25. }
  26. func main() {
  27. var j Root
  28. err := json.Unmarshal([]byte(a), &j)
  29. if err != nil {
  30. log.Fatalf("error parsing JSON: %s\n", err.Error())
  31. }
  32. fmt.Printf("%+v\n", j.Text[0].Sha)
  33. }

以上是代码的翻译部分。

英文:

You can use Go's encoding/json package quite easily. Here is a playground link to the code below.

  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "log"
  6. )
  7. var a = `{
  8. "sometext":[
  9. {
  10. "sha":"1234567",
  11. "message":"hello world",
  12. "author":"varung",
  13. "timestamp":1479445228
  14. }
  15. ]
  16. }`
  17. type Root struct {
  18. Text []*Object `json:"sometext"`
  19. }
  20. type Object struct {
  21. Sha string `json:"sha"`
  22. Message string `json:"message"`
  23. Author string `json:"author"`
  24. Timestamp int `json:"timestamp"`
  25. }
  26. func main() {
  27. var j Root
  28. err := json.Unmarshal([]byte(a), &j)
  29. if err != nil {
  30. log.Fatalf("error parsing JSON: %s\n", err.Error())
  31. }
  32. fmt.Printf("%+v\n", j.Text[0].Sha)
  33. }

答案2

得分: 0

我不知道除了解析JSON之外还有其他的方法。

  1. import (
  2. "time"
  3. "fmt"
  4. "encoding/json"
  5. )
  6. type MyObj struct {
  7. Sha string `json:"sha"`
  8. Message string `json:"message"`
  9. Author string `json:"author"`
  10. Timestamp time.Time `json:"timestamp"`
  11. }
  12. type MyObjs struct {
  13. Objs []MyObj `json:"some text"`
  14. }
  15. func PrintSha(json []byte) {
  16. var myObjs MyObjs
  17. if err := json.Unmarshal(json, &myObjs); err != nil {
  18. panic(err)
  19. }
  20. myObj := myObjs.Objs[0]
  21. fmt.Println(myObj.Sha)
  22. }

以上是代码的翻译。

英文:

I don't know of any way other than unmarshaling the json.

  1. import (
  2. "time"
  3. "fmt"
  4. "encoding/json"
  5. )
  6. type MyObj struct {
  7. Sha string `json:"sha"`
  8. Message string `json:"message"`
  9. Author string `json:"author"`
  10. Timestamp time.Time `json:"timestamp"`
  11. }
  12. type MyObjs struct {
  13. Objs []MyObj `json:"some text"`
  14. }
  15. func PrintSha(json []byte) {
  16. var myObjs MyObjs
  17. if err := json.Unmarshal(json, &myObjs); err != nil {
  18. panic(err)
  19. }
  20. myObj := myObjs[0]
  21. fmt.Println(myObj.sha)
  22. }

huangapple
  • 本文由 发表于 2016年11月22日 01:35:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/40726110.html
匿名

发表评论

匿名网友

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

确定