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

时间:2017-01-11 17:03:05

标签: arrays go struct marshalling

我不确定标题是否准确地解释了我想要做的事情,所以我会尝试尽可能多地提供详细信息。

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

以下是我用于请求的结构:

// 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"`
}

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

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

            ]
        }
    ]
}

如果我有其他任何内容,请求将失败。我没有收到任何错误,但它没有返回任何有用的数据。有关如何完成此任务的任何建议?

*注意:我确实尝试使用[] *参数而不是[]参数,但它给了我相同的结果。

1 个答案:

答案 0 :(得分:0)

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

Realm
相关问题