如何将空的传入JSON数组解组为结构域

时间:2018-04-09 20:59:46

标签: go

给出这样的结构层次结构:

// BasicInfoResult is the result we receive back as part of the basic info command response
type BasicInfoResult struct {
    Username   string `json:"username"`
    MerchantID string `json:"merchant_id"`
    Email      string `json:"email"`
    PublicName string `json:"public_name"`
}

// BasicInfoResponse is a response we get back from the basic info command
type BasicInfoResponse struct {
    Error string `json:"error"`
    Result *BasicInfoResult `json:"result"`
}

收到这样的回复时工作正常:

{  
   "error":"ok",
   "result":{  
      "username":"Username",
      "merchant_id":"User's Merchant ID",
      "email":"account@email.com",
      "public_name":"Account's Public Name", // May be blank if not set!!!
   }
}

但是,如果出现错误,响应格式为: {"error":"This API Key does not have permission to use that command!","result":[]}

然后我的解组打破了这个错误:

Cannot unmarshal array into Go struct field

来自响应的空数组result是问题所在。什么是idiomoatic Go方式回应这个?

0 个答案:

没有答案