如何将空结构体编组为空数组

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

How to marshal an empty struct as an empty array

问题

我不确定标题是否准确地解释了我想做的事情,所以我会尽量提供尽可能多的细节。

我有一个包含嵌套结构的结构体,我正在将其编组并发送到一个API。有一些请求需要我的最低级别结构为空,我需要它的父级参数等于一个空数组,而不是null。如果我在参数上使用omitempty,它会完全从我的请求中删除它,请求将失败。如果我在参数的参数上使用omitempty,它会导致值为null,请求将失败。

这是我用于请求的结构体:

// SubscribeRequest是ICWS请求体的顶级包装器
SubscribeRequest struct {
ClientStateIsFresh bool json:"clientStateIsFresh"
StatisticKeys []StatisticKey json:"statisticKeys"
}

// StatisticKey是我们想从ICWS报告中提取的值
StatisticKey struct {
StatisticIdentifier string json:"statisticIdentifier"
ParameterValueItems []Parameter json:"parameterValueItems"
}

// Parameter是在提取统计数据时应用的过滤器
Parameter struct {
ParameterTypeID string json:"parameterTypeId"
Value string json:"value"
}

我需要编组后的JSON看起来像这样:

{
"clientStateIsFresh": true,
"statisticKeys": [
{
"statisticIdentifier": "inin.system.interaction:ActiveCalls",
"parameterValueItems": []
}
]
}

如果除了这个之外,请求将失败。我没有收到任何错误,但它没有返回任何可用的数据。有关如何实现这一点的任何建议吗?

*注意:我尝试使用[]*Parameter而不是[]Parameter,但结果相同。

英文:

I'm not sure if the title accurately explains what I'm looking to do so I'll try to give as many details as I can.

I have a struct with nested structs that I am marshalling and sending out to an API. There are some requests that require my lowest level struct to be empty and I need its parent parameter to equal an empty array instead of null. If I use omitempty on the parameter, it will completely remove it from my request and the request will fail. If I use omitempty on the parameter's parameters, it causes the value to be null and the request will fail.

Here are the structs I am using for the request:

// SubscribeRequest is the top level wrapper for ICWS request bodies
SubscribeRequest struct {
	ClientStateIsFresh bool           `json:"clientStateIsFresh"`
	StatisticKeys      []StatisticKey `json:"statisticKeys"`
}

// StatisticKey is a value we want to pull from ICWS reporting
StatisticKey struct {
	StatisticIdentifier string       `json:"statisticIdentifier"`
	ParameterValueItems []Parameter `json:"parameterValueItems"`
}

// Parameter is a filter applied when pulling statistics
Parameter struct {
	ParameterTypeID string `json:"parameterTypeId"`
	Value           string `json:"value"`
}

And I need the marshalled JSON to look like this:

{
    "clientStateIsFresh":true,
    "statisticKeys":
    [
	    {
		    "statisticIdentifier":"inin.system.interaction:ActiveCalls",
		    "parameterValueItems":
		    [

		    ]
	    }
    ]
}

If I have anything other than this, the request fails. I don't get any errors, but it doesn't return any usable data. Any suggestions on how to accomplish this?

*Note: I did try using []*Parameter instead of []Parameter, but it gave me the same result.

答案1

得分: 0

如果你想要一个空数组,你需要提供一个空的切片。

StatisticKey{
StatisticIdentifier: "id.string",
ParameterValueItems: []Parameter{},
}

英文:

If you want an empty array, you have to provide an empty slice.

StatisticKey{
	StatisticIdentifier: "id.string",
	ParameterValueItems: []Parameter{},
}

huangapple
  • 本文由 发表于 2017年1月12日 01:03:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/41596613.html
匿名

发表评论

匿名网友

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

确定