dongtui0650 2016-03-25 17:25
浏览 175
已采纳

Golang JSON结构为小写不起作用

I have a struct:

type Credentials struct {
    Username    string  `json:"username"`
    Password    string  `json:"password"`
    ApplicationId   string  `json:"application_id"`
    ApplicationKey  string  `json:"application_key"`
}

And I've tagged my fields to lowercase them.

However, whenever I include the application tags, those fields become null, i.e. on my POST request I get

{ application_id: '',
  application_key: '',
  password: 'myPassword',
  username: 'myUsername' 
}

But if I remove either of the tag (so remove ApplicatinonId or ApplicationKey tag), then that field does show up

Here is how I set my struct:

func getCredentials() Credentials {
    raw, err := ioutil.ReadFile(os.Getenv("BASE_PATH") + FILE_Credentials)
    if err != nil {
        log.Warn("Could not read credentials file: %s", err.Error())
        os.Exit(1)
    }

    var credentials Credentials
    json.Unmarshal(raw, &credentials)
    return credentials
}

My credential json file is:

{
  "Username": "myUsername",
  "Password": "myPassowrd",
  "ApplicationId": "someID",
  "ApplicationKey": "someString"
}

Then, I post my data with:

credentials := getCredentials()
url := GetLoginURL()

resp, body, requestErr := gorequest.New().
    Post(url).
    Send(credentials).
    End()

But on the server, I get both application_id and application_key as empty strings. But if I remove the corresponding tag, then that field is posted

  • 写回答

2条回答 默认 最新

  • dongnai3960 2016-03-25 20:19
    关注

    Based off the example file your struct in Go should look like this;

    type Credentials struct {
        Username    string  `json:"Username"`
        Password    string  `json:"Password"`
        ApplicationId   string  `json:"ApplicationId"`
        ApplicationKey  string  `json:"ApplicationKey"`
    }
    

    You could also approach this from the other end and modify the entries in your file to look like this;

    {
      "Username": "myUsername",
      "Password": "myPassowrd",
      "application_id": "someID",
      "application_key": "someString"
    }
    

    However, it's more often the case that you can't change the data you're receiving (like when calling a third party API) so you usually end up changing your source. Since you control the file and the API wants lower case I'd recommend changing the files contents to match what you send the API. The other option which is sometimes necessary is to use another type and provide a conversion helper (assuming you controlled neither the file nor the API, you'd need different types for each). The Go encoding packages are very strict. You may be used to things like json.NET which will assign near matches, that isn't the case here. Anything less than an exact match will yield an error from Unmarshal.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里