登录
首页 >  Golang >  Go问答

利用类型解组来递归解析 JSON 数据

来源:stackoverflow

时间:2024-03-02 20:36:28 327浏览 收藏

有志者,事竟成!如果你在学习Golang,那么本文《利用类型解组来递归解析 JSON 数据》,就很适合你!文章讲解的知识点主要包括,若是你对本文感兴趣,或者是想搞懂其中某个知识点,就请你继续往下看吧~

问题内容

我有一个具有以下形式的 json:

{
  "Filter": {
    "Filters": [
      {
        "Filter": {
           "someKey": "someValue1",
        },
        "FilterType": "trait"
      },
      {
        "Filter": {
           "someKey": "someValue2",
        },
        "FilterType": "trait"
      }
    ]
  },
  "FilterType": "and" // this can be an or too
}

每个过滤器都有一个类型,该类型映射到我在代码中获得的特定结构。正如您所看到的,这具有递归性质:过滤器可以是带有多个子过滤器的 and (也可以是 ands 或 ors 等)。

因此,根据类型,我需要递归地将其映射到正确的结构。有什么建议吗?我研究过使用映射结构,但即使如此,代码也变得非常糟糕。

请注意,此示例仅使用 andtrait 过滤器,但假设还有更多类似 or 的过滤器


正确答案


这种类型的多态 json 很常见,并且通常不容易处理。我在这里解释的是一种使用单独的结构来实现此目的的方法,该结构仅用于封送,并依赖于后处理来构造过滤器。

您可以定义一个包含过滤器所有可能字段的 filtermarshal 结构:

type filtermarshal struct {
   filtertype string `json:"filtertype"`
   filters []*filtermarshal `json:"filters"`
   filter   *filtermarshal `json:"filter"`
   somekey  string `json:"somekey"`
   ...
}

此实现假设不存在名称冲突。例如,如果过滤器类型将 filter 字段作为一种类型,而另一种类型将 filter 字段作为不兼容类型,则这将不起作用,您必须使用其他方法。

当您解组 filtermarshal 中的 json 时,您将获得与输入 json 匹配的对象树。然后,您可以构建实际的过滤器结构:

func unmarshalfilter(input *filtermarshal) filter {
   switch input.filtertype {
     case "and": 
        // process input.filters recursively
       return andfilter{filters: processfilters(input.filters))
     case "trait": 
       return traitfilter{somekey: input.somekey}
   }
}

在没有澄清功能规范的情况下,您需要使用此结构来表示与您在问题中发布的 json 相近的内容。

type filtercontainer struct {
    filter     map[string]interface{} `json:"filter,omitempty"`
    filters    []filtercontainer `json:"filters,omitempty"`
    filtertype string `json:"filtertype"`
}

你的过滤器将像这样在 go 中初始化

f := filtercontainer{
    filters: []filtercontainer{
        {
            filter:     map[string]interface{}{"somekey": "somevalue1"},
            filtertype: "trait",
        },
        {
            filter:     map[string]interface{}{"somekey": "somevalue2"},
            filtertype: "trait",
        },
    },
    filtertype: "and",
}

当过滤器被编组为 json 并漂亮地打印时,它们看起来像这样

{
  "filters": [
    {
      "filter": {
        "somekey": "somevalue1"
      },
      "filtertype": "trait"
    },
    {
      "filter": {
        "somekey": "somevalue2"
      },
      "filtertype": "trait"
    }
  ],
  "filtertype": "and"
}

上面的 json 与您问题中的 json 不完全匹配;它缺少顶级 filter 字段。我认为我在这个答案中提出的结构将简化实现以及心智模型。

https://go.dev/play/p/6cLPT28DG2O 的 go 演示中尝试一下

如果您确实非常需要匹配问题中的 json

这是您需要的结构

type filtercontainer struct {
    filter     interface{} `json:"filter,omitempty"` // either a map[string]interface{} or a filtercontainer
    filters    []filtercontainer `json:"filters,omitempty"`
    filtertype string `json:"filtertype,omitempty"`
}

它是这样填充的

f := FilterContainer{
    Filter: FilterContainer{
        Filters: []FilterContainer{
            {
                Filter:     map[string]interface{}{"someKey": "someValue1"},
                FilterType: "trait",
            },
            {
                Filter:     map[string]interface{}{"someKey": "someValue2"},
                FilterType: "trait",
            },
        },
    },
    FilterType: "and",
}

https://go.dev/play/p/uB_LmJc6NGH 的 go 演示中尝试一下

以上就是本文的全部内容了,是否有顺利帮助你解决问题?若是能给你带来学习上的帮助,请大家多多支持golang学习网!更多关于Golang的相关知识,也可关注golang学习网公众号。

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>