Go语言字符串处理和JSON

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

go string handling and JSON

问题

新的Gopher在这里!
我正在使用Go语言处理一个REST API,并且现在正在解析我的第一个返回的JSON,但似乎遇到了一些问题。

首先,API调用的原始返回结果如下:

spew.Dump(body)

(string) (len=205) "{\"return\": {\n    \"apiMajorVersion\": 3,\n    \"apiMinorVersion\": 0,\n    \"esmMajorVersion\": 9,\n    \"esmMinorVersion\": 5,\n    \"esmPatch\": \"MR7\",\n    \"esmRevision\": 0,\n    \"esmVersionString\": \"9.5.0 20150908\"\n}}"

字符串中嵌入了反斜杠和换行符。如果我打印它:

fmt.Println(body)

{"return": {
    "apiMajorVersion": 3,
    "apiMinorVersion": 0,
    "esmMajorVersion": 9,
    "esmMinorVersion": 5,
    "esmPatch": "XX7",
    "esmRevision": 0,
    "esmVersionString": "9.5.0 20150908"
}}

然后我得到了有效的JSON。

如果我尝试将其解组为结构体,我无法正确地获取结构体中的值。

type ESMVersionStruct struct {
    APIMajorVersion     int8 `json:"return>apiMajorVersion"`
    APIMinorVersion     int8 `json:"apiMinorVersion"`
    ESMMajorVersion     int8 `json:"esmMajorVersion"`
    ESMMinorVersion     int8 `json:"esmMinorVersion"`
    ESMPatch            string `json:"esmPatch"`
    ESMRevision         int8 `json:"esmRevision"`
    ESMVersionString    string `json:"esmVersionString"`
}

我尝试过在返回中指定对象,也尝试过不指定。

jsonData := []byte(body)
var ESMVersion ESMVersionStruct
json.Unmarshal(jsonData, &ESMVersion)

fmt.Println(ESMVersion.APIMajorVersion)
fmt.Print(ESMVersion.APIMinorVersion)

最后两个都返回了空值。

提前感谢您对此问题的任何帮助!

英文:

New Gopher here!
I am working with a rest API in go, and I am now working on parsing out my first return JSON and seem to be having a bit of trouble.

First the raw return from the API call nets me this:

spew.Dump(body)

(string) (len=205) "{\"return\": {\n    \"apiMajorVersion\": 3,\n    \"apiMinorVersion\": 0,\n    \"esmMajorVersion\": 9,\n    \"esmMinorVersion\": 5,\n    \"esmPatch\": \"MR7\",\n    \"esmRevision\": 0,\n    \"esmVersionString\": \"9.5.0 20150908\"\n}}"

With all the backslashes and newlines embedded in the string. If I print it

fmt.Println(body)

{"return": {
    "apiMajorVersion": 3,
    "apiMinorVersion": 0,
    "esmMajorVersion": 9,
    "esmMinorVersion": 5,
    "esmPatch": "XX7",
    "esmRevision": 0,
    "esmVersionString": "9.5.0 20150908"
}}

Then I get valid json.

If I try and unmarshal it to the struct I don't get the values in the struct properly.

type ESMVersionStruct struct {
    APIMajorVersion 	int8 `json:"return>apiMajorVersion"`
    APIMinorVersion 	int8 `json:"apiMinorVersion"`
    ESMMajorVersion 	int8 `json:"esmMajorVersion"`
    ESMMinorVersion 	int8 `json:"esmMinorVersion"`
    ESMPatch			string `json:"esmPatch"`
    ESMRevision			int8 `json:"esmRevision"`
    ESMVersionString 	string `json:"esmVersionString"`
}

I have tried both specifying the object in the return and not.

jsonData := []byte(body)
var ESMVersion ESMVersionStruct
json.Unmarshal(jsonData, &ESMVersion)

fmt.Println(ESMVersion.APIMajorVersion)
fmt.Print(ESMVersion.APIMinorVersion)

Both of the last two return the null value.

Thanks in advance for any help with this one!

答案1

得分: 2

你的类型应该是:

type ESMVersionStruct struct {
    Return struct {
        APIMajorVersion     int8 `json:"apiMajorVersion"`
        APIMinorVersion     int8 `json:"apiMinorVersion"`
        ESMMajorVersion     int8 `json:"esmMajorVersion"`
        ESMMinorVersion     int8 `json:"esmMinorVersion"`
        ESMPatch            string `json:"esmPatch"`
        ESMRevision         int8 `json:"esmRevision"`
        ESMVersionString    string `json:"esmVersionString"`
    } `json:"return"`
}

也就是说,你的结构体嵌套在另一个名为 Return 的结构体中。

英文:

Your type should be:

type ESMVersionStruct struct {
    Return struct {
        APIMajorVersion     int8 `json:"apiMajorVersion"`
        APIMinorVersion     int8 `json:"apiMinorVersion"`
        ESMMajorVersion     int8 `json:"esmMajorVersion"`
        ESMMinorVersion     int8 `json:"esmMinorVersion"`
        ESMPatch            string `json:"esmPatch"`
        ESMRevision         int8 `json:"esmRevision"`
        ESMVersionString    string `json:"esmVersionString"`
    } `json:"return"`
}

That is, your struct embedded in another Return struct.

huangapple
  • 本文由 发表于 2015年9月22日 01:32:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/32701281.html
匿名

发表评论

匿名网友

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

确定