在Go语言规范中,它提到了标签的简要概述:
字段声明后面可以跟一个可选的字符串文字标记,该标记成为相应字段声明中所有字段的属性.标签通过反射界面可见,但否则将被忽略.
Run Code Online (Sandbox Code Playgroud)// A struct corresponding to the TimeStamp protocol buffer. // The tag strings define the protocol buffer field numbers. struct { microsec uint64 "field 1" serverIP6 uint64 "field 2" process string "field 3" }
这是一个非常简短的解释IMO,我想知道是否有人可以提供我这些标签的用途?
是否可以用未知字段解组 JSON?该字段名称始终不同,但结构是相同的。JSON 是 POST 请求的结果。
我的代码:
package main
import "fmt"
import "encoding/json"
var body = []byte(`{
"unknown_field": {
"known_field_1": [[1,2,3,4,5],[10,20,30,40,50],[100,200,300,400,500]],
"known_field_2": [[11,21,31,41,51]],
"known_field_3": [[12,22,32,42,52],[14,44,34,44,54]]
}
}`)
type MyStruct struct {
MainData map[string]mData `json:"-"`
}
type mData struct {
knownField1 [][5]int `json:"known_field_1"`
knownField2 [][5]int `json:"known_field_2"`
knownField3 [][5]int `json:"known_field_3"`
}
func NewMyStruct() MyStruct {
ms := MyStruct{}
ms.MainData = make(map[string]mData)
return ms
}
func main() {
myStruct := NewMyStruct()
if err := json.Unmarshal(body, &myStruct); err != nil {
panic(err)
}
fmt.Println(myStruct) // …Run Code Online (Sandbox Code Playgroud) 我有这个JSON数据:
{
"InfoA" : [256,256,20000],
"InfoB" : [256,512,15000],
"InfoC" : [208,512,20000],
"DEFAULT" : [256,256,20000]
}
Run Code Online (Sandbox Code Playgroud)
使用JSON-to-Go,我得到了这个Go类型定义:
type AutoGenerated struct {
InfoA []int `json:"InfoA"`
InfoB []int `json:"InfoB"`
InfoC []int `json:"InfoC"`
DEFAULT []int `json:"DEFAULT"`
}
Run Code Online (Sandbox Code Playgroud)
使用此代码(play.golang.org)
package main
import (
"encoding/json"
"fmt"
"os"
"strings"
)
func main() {
type paramsInfo struct {
InfoA []int `json:"InfoA"`
InfoB []int `json:"InfoB"`
InfoC []int `json:"InfoC"`
DEFAULT []int `json:"DEFAULT"`
}
rawJSON := []byte(`{
"InfoA" : [256,256,20000],
"InfoB" : [256,512,15000],
"InfoC" : [208,512,20000],
"DEFAULT" …Run Code Online (Sandbox Code Playgroud) 我试图使用Golang将API的响应映射到结构.
我在浏览器中查看链接时返回的JSON如下:
{
"GBP": 657.54
}
Run Code Online (Sandbox Code Playgroud)
我只想将它映射到一个简单的结构,如下所示:
type Price struct {
Name string
Value float64
}
Run Code Online (Sandbox Code Playgroud)
这是我目前的代码.
func FetchCoinPrice(fsym string, tsyms string) Price {
url := fmt.Sprintf("https://min-api.cryptocompare.com/data/price?fsym=" + fsym + "&tsyms=" + tsyms)
fmt.Println("Requesting data from " + url)
price := Price{}
// getting the data using http
request, err := http.Get(url)
if err != nil {
log.Fatal(err.Error())
}
// Read the response body using ioutil
body, err := ioutil.ReadAll(request.Body)
if err != nil {
log.Fatal(err.Error())
}
defer request.Body.Close()
if …Run Code Online (Sandbox Code Playgroud) 有一个关于输入数据的示例。
\n\n{\n "status": "OK",\n "status_code": 100,\n "sms": {\n "79607891234": {\n "status": "ERROR",\n "status_code": 203,\n "status_text": "\xd0\x9d\xd0\xb5\xd1\x82 \xd1\x82\xd0\xb5\xd0\xba\xd1\x81\xd1\x82\xd0\xb0 \xd1\x81\xd0\xbe\xd0\xbe\xd0\xb1\xd1\x89\xd0\xb5\xd0\xbd\xd0\xb8\xd1\x8f"\n },\n "79035671233": {\n "status": "ERROR",\n "status_code": 203,\n "status_text": "\xd0\x9d\xd0\xb5\xd1\x82 \xd1\x82\xd0\xb5\xd0\xba\xd1\x81\xd1\x82\xd0\xb0 \xd1\x81\xd0\xbe\xd0\xbe\xd0\xb1\xd1\x89\xd0\xb5\xd0\xbd\xd0\xb8\xd1\x8f"\n },\n "79105432212": {\n "status": "ERROR",\n "status_code": 203,\n "status_text": "\xd0\x9d\xd0\xb5\xd1\x82 \xd1\x82\xd0\xb5\xd0\xba\xd1\x81\xd1\x82\xd0\xb0 \xd1\x81\xd0\xbe\xd0\xbe\xd0\xb1\xd1\x89\xd0\xb5\xd0\xbd\xd0\xb8\xd1\x8f"\n }\n },\n "balance": 2676.18\n}\nRun Code Online (Sandbox Code Playgroud)\n\n{\n "status": "OK",\n "status_code": 100,\n "sms": {\n "79607891234": {\n "status": "ERROR",\n "status_code": 203,\n "status_text": "\xd0\x9d\xd0\xb5\xd1\x82 \xd1\x82\xd0\xb5\xd0\xba\xd1\x81\xd1\x82\xd0\xb0 \xd1\x81\xd0\xbe\xd0\xbe\xd0\xb1\xd1\x89\xd0\xb5\xd0\xbd\xd0\xb8\xd1\x8f"\n },\n "79035671233": {\n "status": "ERROR",\n "status_code": 203,\n "status_text": "\xd0\x9d\xd0\xb5\xd1\x82 \xd1\x82\xd0\xb5\xd0\xba\xd1\x81\xd1\x82\xd0\xb0 \xd1\x81\xd0\xbe\xd0\xbe\xd0\xb1\xd1\x89\xd0\xb5\xd0\xbd\xd0\xb8\xd1\x8f"\n },\n "79105432212": {\n "status": "ERROR",\n "status_code": …Run Code Online (Sandbox Code Playgroud)