英文:
Golang returning a default array of structs
问题
当我使用curl调用API时,我得到的响应如下所示:
[{"Hits":25,"Name":"HIT"},{"Hits":87,"Name":"MISS"},{"Hits":15,"Name":"EXPIRED"}]
当响应返回0值时,即curl返回以下内容:
[{"Hits":0,"Name":"HIT"},{"Hits":0,"Name":"MISS"},{"Hits":0,"Name":"EXPIRED"}]
实际上,在代码中对JSON进行解组后,我得到的是:
[{1 NONE}]
我实际上需要带有0值的键名,因为我最终要将其写入文件,供另一个系统使用。
为了解决这个问题,如果返回值是[{1 NONE}]
,我创建了一个默认数组。
例如:
type CHData struct {
Hits int `json:"Hits"`
Name string `json:"Name"`
}
func apiResponse() (apiResponseData, error) {
//调用API并返回响应的代码 {
...
json.Unmarshal([]byte(body), &apiResponseData)
..
}
if len(apiReturneddata) <= 1 { // 检查是否为[{1 NONE}]值
//如果API返回0值,则创建默认数组
array1 := CHData{Hits: 0, Name: "HIT"}
array2 := CHData{Hits: 0, Name: "MISS"}
array3 := CHData{Hits: 0, Name: "EXPIRED"}
//将每个JSON添加到JSON数组中
var mockData []CHData
mockData = append(mockData, array1)
mockData = append(mockData, array2)
mockData = append(mockData, array3)
log.Println(mockData)
i, _ := json.Marshal(mockData)
//log.Println(string(i))
//用[{"Hits":0,"Name":"HIT"},{"Hits":0,"Name":"MISS"},{"Hits":0,"Name":"EXPIRED"}]覆盖"[{1 NONE}]"
json.Unmarshal([]byte(i), &apiResponseData)
}
return apiResponseData
}
- ^ 这个方法可以工作,但是否有更好的方法来做这个?它似乎很冗长,我只有3个结构体在这个数组中,但我可能会有更多吗?
- 为什么它返回
[{1 NONE}]
而不是键值对结构体?
英文:
When I curl an api I get back a response like this:
[{"Hits":25,"Name":"HIT"},{"Hits":87,"Name":"MISS"},{"Hits":15,"Name":"EXPIRED"}]
When a response comes back with 0 values ie, the curl returns this :
[{"Hits":0,"Name":"HIT"},{"Hits":0,"Name":"MISS"},{"Hits":0,"Name":"EXPIRED"}]
What I actually get after unmarshal the json in code with a 0 value response is:
[{1 NONE}]
I actually need the Key names with the 0 values because I'm writing this out to a file eventually to be consumed by another system.
To get around this I created a default array if the return gives me a [{1 NONE}]
For example:
type CHData struct {
Hits int `json:"Hits"`
Name string `json:"Name"`
}
func apiResponse () apiResponseData,err{
//code that calls the api and returns the response {
...
json.Unmarshal([]byte(body), &apiResponseData)
..
}
if len(apiReturneddata) <= 1 { // checks for [{1 NONE}] value
//Default array if api returns with 0 values
array1 := CHData{Hits: 0, Name: "HIT"}
array2 := CHData{Hits: 0, Name: "MISS"}
array3 := CHData{Hits: 0, Name: "EXPIRED"}
//adding each json into the array of json's
var mockData []CHData
mockData = append(mockData, array1)
mockData = append(mockData, array2)
mockData = append(mockData, array3)
log.Println(mockData)
i, _ := json.Marshal(mockData)
//log.Println(string(i))
//Overwrite the "[{1 NONE}]" with [{"Hits":0,"Name":"HIT"},{"Hits":0,"Name":"MISS"},{"Hits":0,"Name":"EXPIRED"}]
json.Unmarshal([]byte(i), &apiResponseData)
}
return apiResponseData
}
- ^ This works BUT is there a better way to do this it seems very verbose, I only have 3 structs in this array but I could have a lot more?
- Why does does it return
[{1 NONE}]
anyway and not the Key:Value struts?
答案1
得分: 0
我将假设apiResponseData是[]CHData。
你应该检查响应是否为none(1个条目,1个名为None的命中等)。
如果是,请使用默认响应。
type apiResponseData []CHData
var defaultAPIResponseData = apiResponseData{
{0, "HIT"},
{0, "MISS"},
{0, "EXPIRED"},
}
func (r apiResponseData) IsNone() bool {
return len(r) == 1 && r[0].Name == "None";
}
英文:
I will assume apiResponseData is []CHData
You should check if the response is none ( 1 entry, 1 hit named None etc)
If yes use a default response.
type apiResponseData []CHData
var defaultAPIResponseData = apiResponseData{
{0, "HIT"},
{0, "MISS"},
{0, "EXPIRED"},
}
func (r apiResponseData) IsNone() bool {
return len(r) == 1 && r[0].Name == "None"
}
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论