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 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?