gpt4 book ai didi

json - Reflect vs Regex 检查 Golang 中的空 JSON 数组属性

转载 作者:数据小太阳 更新时间:2023-10-29 03:43:50 25 4
gpt4 key购买 nike

我从我们的客户端收到一个属性为空的 JSON 数组:

[
{},{},{},{},{}
]

通常它看起来像这样例如:

[
{"Name": "foo", "Text": "Costumer"},
{"Name": "foo", "Text": "Employer"},
{"Name": "foo", "Text": "Costumer"},
{"Name": "foo", "Text": "Emplopyer"},
{"Name": "foo", "Text": "Employer"}
]

据我的老师所说,有两种可能的方法来检查这些空属性:

正则表达式包 && 反射包

我应该使用哪个来提高性能?

请解释为什么你会选择那个包而不是其他包

最佳答案

最高效和防错的方法是使用 json's Decoder.Token 自己手动解析 JSON token 。及相关方法。 这避免了 json 包对 Reflect 的正常使用完全(因为您没有解码为任意结构),并且它避免了容易出错的正则表达式。它的性能也可能优于正则表达式,但需要一个基准来确定。

但这将是一些冗长且可以说是丑陋的代码。

关于json - Reflect vs Regex 检查 Golang 中的空 JSON 数组属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48299385/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com