如何在Go语言中从大型JSON中获取数据

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

How to get data from big json in go

问题

我有这个JSON,现在我需要获取字段"textValue"的值为["BigFoot Inc."]。

我现在能够获取到的只是自定义数据数组中的数据,但是我无法获取特定字段的数据。我尝试获取数据,但是一切都很糟糕,我很高兴能得到你的帮助。

JSON:

  "id": 759,
  "author": {
    "id": 1,
    "name": "Gogi Na Vole",
    "completedOn": "Never",
    "custom_fields": [
      {
        "id": 86,
        "name": "property_86",
        "label": "Type of Question",
        "value": [
          "90"
        ],
        "textValue": [
          "Other"
        ]
      },
      {
        "id": 69,
        "name": "property_69",
        "label": "Client",
        "value": [
          "82"
        ],
        "textValue": [
          "BigFoot Inc."
        ]
      }
    ]
  }
}```

我的代码:
```package main

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"net/url"
)

var body = []byte(`JSON HERE
`)

type TiketData struct {
	Struct here
}

func main() {
var data TiketData

	json_err := json.Unmarshal(body, &data)
	if json_err != nil {
		fmt.Println(json_err)
	}

	for _, customFields := range data.CustomFields {
		fmt.Println(fmt.Sprintf("%#v", customFields))
	}
}
英文:

I have that json and now i need to get that field "textValue": [
"BigFoot Inc."
]

All that I have now been able to get is data from an array of custom data. But I can't get data from a specific field. I tried to get the data, but everything is very sad, I will be glad for your help

JSON:

  "id": 759,
  "author": {
    "id": 1,
    "name": "Gogi Na Vole",
  "completedOn": "Never",
  "custom_fields": [
    {
      "id": 86,
      "name": "property_86",
      "label": "Type of Question",
      "value": [
        "90"
      ],
      "textValue": [
        "Other"
      ]
    },
    {
      "id": 69,
      "name": "property_69",
      "label": "Client",
      "value": [
        "82"
      ],
      "textValue": [
        "BigFoot Inc."
      ]
    }
  ]
}

MyCode:


import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"net/url"
)

var body = []byte(`JSON HERE
`)

type TiketData struct {
	Struct here
}

func main() {
var data TiketData

	json_err := json.Unmarshal(body, &data)
	if json_err != nil {
		fmt.Println(json_err)
	}

	for _, customFields := range data.CustomFields {
		fmt.Println(fmt.Sprintf("%#v", customFields))
	}
}

答案1

得分: 0

首先,你需要为你的JSON定义struct type。我使用了自动生成的struct type,但你也可以将其分成多个部分。然后,只需按照相同的步骤进行解组和访问变量。希望这能解决你的问题。

package main

import (
	"encoding/json"
	"fmt"
)

var j = `{
  "id": 759,
  "author": {
    "id": 1,
    "name": "Gogi Na Vole",
    "completedOn": "Never",
    "custom_fields": [
      {
        "id": 86,
        "name": "property_86",
        "label": "Type of Question",
        "value": [
          "90"
        ],
        "textValue": [
          "Other"
        ]
      },
      {
        "id": 69,
        "name": "property_69",
        "label": "Client",
        "value": [
          "82"
        ],
        "textValue": [
          "BigFoot Inc."
        ]
      }
    ]
  }
}`

type Data struct {
	Id     int `json:"id"`
	Author struct {
		Id           int    `json:"id"`
		Name         string `json:"name"`
		CompletedOn  string `json:"completedOn"`
		CustomFields []struct {
			Id        int      `json:"id"`
			Name      string   `json:"name"`
			Label     string   `json:"label"`
			Value     []string `json:"value"`
			TextValue []string `json:"textValue"`
		} `json:"custom_fields"`
	} `json:"author"`
}

func main() {
	var data *Data
	err := json.Unmarshal([]byte(j), &data)
	if err != nil {
		fmt.Println(err.Error())
		return
	}

	textVal := data.Author.CustomFields[1].TextValue
	fmt.Println(textVal)
}

输出将是:

[BigFoot Inc.]

Go Playground

英文:

Firstly, you need to define struct type for your JSON. I used auto-generated struct type but you can divide into multiple ones. Then, just apply the same steps for unmarshalling and access the variable. I hope it will solve your problem.

package main

import (
	"encoding/json"
	"fmt"
)

var j = `{
  "id": 759,
  "author": {
    "id": 1,
    "name": "Gogi Na Vole",
    "completedOn": "Never",
    "custom_fields": [
      {
        "id": 86,
        "name": "property_86",
        "label": "Type of Question",
        "value": [
          "90"
        ],
        "textValue": [
          "Other"
        ]
      },
      {
        "id": 69,
        "name": "property_69",
        "label": "Client",
        "value": [
          "82"
        ],
        "textValue": [
          "BigFoot Inc."
        ]
      }
    ]
  }
}`

type Data struct {
	Id     int `json:"id"`
	Author struct {
		Id           int    `json:"id"`
		Name         string `json:"name"`
		CompletedOn  string `json:"completedOn"`
		CustomFields []struct {
			Id        int      `json:"id"`
			Name      string   `json:"name"`
			Label     string   `json:"label"`
			Value     []string `json:"value"`
			TextValue []string `json:"textValue"`
		} `json:"custom_fields"`
	} `json:"author"`
}

func main() {
	var data *Data
	err := json.Unmarshal([]byte(j), &data)
	if err != nil {
		fmt.Println(err.Error())
		return
	}

	textVal := data.Author.CustomFields[1].TextValue
	fmt.Println(textVal)
}

Output will be:

[BigFoot Inc.]

Go Playground

huangapple
  • 本文由 发表于 2021年11月15日 18:35:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/69972993.html
匿名

发表评论

匿名网友

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

确定