英文:
Make all properties lower case or upper case using reflect?
问题
我正在从客户端接收未知的 JSON 数据,并将其解析为接口类型:
var f interface{}
err := json.Unmarshal(b, &f)
如何将 f
中的所有键转换为小写键?我需要将这个 f
保存到 MongoDB,并且我需要进行一些查询,但我希望避免错误,以防有人发送相同的 JSON 数据但使用了大写键。
英文:
I am receiving unknown json from client and I parse to interface like
var f interface{}
err := json.Unmarshal(b, &f)
How to make all keys in f to be lower keys ?
I have to save this f to mongo and I need to make some queries but I want to avoid mistake if somebody send uppercase same json.
答案1
得分: 5
这是一种方法:
var v any
err := json.Unmarshal(b, &v)
v = lower(v)
其中lower
函数定义如下:
func lower(v any) any {
switch v := v.(type) {
case []any:
lv := make([]any, len(v))
for i := range v {
lv[i] = lower(v[i])
}
return lv
case map[string]any:
lv := make(map[string]any, len(v))
for mk, mv := range v {
lv[strings.ToLower(mk)] = mv
}
return lv
default:
return v
}
}
lower
函数通过递归调用自身来处理嵌套的JSON对象和数组中的键转换。
如果你知道你正在处理的对象没有嵌套(对象字段不包含数组或其他对象),那么你可以将上述lower
函数中的map case内联:
var v map[string]any
err := json.Unmarshal(b, &v)
lv := make(map[string]any, len(v))
for mk, mv := range v {
lv[strings.ToLower(mk)] = mv
}
v = lv
英文:
Here's one way to do it:
var v any
err := json.Unmarshal(b, &v)
v = lower(v)
where lower is:
func lower(v any) any {
switch v := v.(type) {
case []any:
lv := make([]any, len(v))
for i := range v {
lv[i] = lower(v[i])
}
return lv
case map[string]any:
lv := make(map[string]any, len(v))
for mk, mv := range v {
lv[strings.ToLower(mk)] = mv
}
return lv
default:
return v
}
}
The lower
function calls itself recursively to handle key conversion in nested JSON objects and arrays.
If you know that you are working with an object without nesting (the object fields do not contain arrays or other objects), then you can inline the map case from the lower
function above:
var v map[string]any
err := json.Unmarshal(b, &v)
lv := make(map[string]any, len(v))
for mk, mv := range v {
lv[strings.ToLower(mk)] = mv
}
v = lv
答案2
得分: 3
将会是 map[string]interface{}
,所以遍历它并简单地将键转换为小写。
var f map[string]interface{}
...
converted := make(map[string]interface{}, len(f))
for k, v := range f {
converted[strings.ToLower(k)] = v
}
翻译结果:
var f map[string]interface{}
...
converted := make(map[string]interface{}, len(f))
for k, v := range f {
converted[strings.ToLower(k)] = v
}
英文:
It will be map[string]interface{}
so go over it and simply convert keys to lowercase.
var f map[string]interface{}
...
converted := make(map[string]interface{}, len(f))
for k, v := range f {
converted[strings.ToLower(k)] = v
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论