如何通过循环 JSON 显示 golang 中丢失的键?
来源:stackoverflow
时间:2024-04-04 13:06:38 279浏览 收藏
golang学习网今天将给大家带来《如何通过循环 JSON 显示 golang 中丢失的键?》,感兴趣的朋友请继续看下去吧!以下内容将会涉及到等等知识点,如果你是正在学习Golang或者已经是大佬级别了,都非常欢迎也希望大家都能给我建议评论哈~希望能帮助到大家!
我正在为我的应用程序构建一个翻译工具。我有 2 个不同的 json 文件(en.json、fr.json),其中包含我的键和值。
en.json 文件是引用文件,这意味着如果该文件中存在的键不存在,则需要将其添加到 fr.json 文件中。 有关更多详细信息,如果密钥存在于 en.json 中而不是存在于 fr.json 中,我们需要显示,但如果密钥仅存在于 fr.json 中,则它不算在内,我们不会在屏幕上显示任何内容。如果密钥已经存在于两者中,我们不需要在屏幕上显示任何内容。
en.json 文件是具有以下键的引用文件:
{ "i18n-outterjson": { "i18n-value1": "value1", "i18n-value2": "value2", "i18n-dad" : "dady" }, "innerjson2":"nonevalue", "hello" : "good morning", "onlykey" : "only key", "secondekey" : "second key", "mother": { "daugther": "fille" } }
fr.json 文件是目标文件。使用以下键:
{ "i18n-outterjson": { "i18n-value1": "value1", "i18n-value2": "value2" }, "i18n-hello" : "bonjour", "i18n-variation" : "variation", "alouette" : "cacahouete", "innerjson2":"pas de valeur", "hello" : "bonjour" }
我在 golang 中的代码是:
fileenglish := getallpathreffile(); // to open the reference file files := getalli18nfiles(); // to open the french file for _, fileref := range fileenglish { mapref := readjsonfile(constants.pathreffile + getridofextension(fileref.name())) for _, filetarget := range files { maptarget := readjsonfile(constants.i18npath + getridofextension(filetarget.name())); maptarget = missingkey(mapref, maptarget) // range loop to identify the missing keys in the different files for key, _ := range mapref { // mapref is the reference map of the reference file en.json if mapref[key] != maptarget[key] { // maptarget is map of the target file fr.json t := strings.replace(key, key, "", -1) _ = t fmt.println("the missing keys are: ", key) } } } }
我尝试比较每个键,在某些情况下我们有一个层次结构,我尝试识别丢失的键。 我需要获得这个结果:
i18n-dad mother daughter onlykey secondekey
但是使用我当前的代码,我遇到了这个错误:comparing uncomparable type map[string]interface {}
。
如何正确识别按键并将其打印在屏幕上?
解决方案
下面是一个简单示例,使用 https://github.com/go-test/deep 展示了法语 json 中缺少的内容。
package main import ( "encoding/json" "fmt" "strings" "github.com/go-test/deep" ) type data struct { i18noutterjson struct { i18nvalue1 string `json:"i18n-value1"` i18nvalue2 string `json:"i18n-value2"` i18ndad string `json:"i18n-dad"` } `json:"i18n-outterjson"` innerjson2 string `json:"innerjson2"` hello string `json:"hello"` onlykey string `json:"onlykey"` secondekey string `json:"secondekey"` mother struct { daugther string `json:"daugther"` } `json:"mother"` } func main() { enjson := []byte(`{"i18n-outterjson":{"i18n-value1":"value1","i18n-value2":"value2","i18n-dad":"dady"},"innerjson2":"nonevalue","hello":"good morning","onlykey":"only key","secondekey":"second key","mother":{"daugther":"fille"}}`) frjson := []byte(`{"i18n-outterjson":{"i18n-value1":"value1","i18n-value2":"value2"},"i18n-hello":"bonjour","i18n-variation":"variation","alouette":"cacahouete","innerjson2":"pas de valeur","hello":"bonjour"}`) var en, fr data err := json.unmarshal(enjson, &en) if err != nil { panic(err) } err = json.unmarshal(frjson, &fr) if err != nil { panic(err) } diff := deep.equal(en, fr) for _, d := range diff { if strings.hassuffix(d, "!= ") { fmt.printf("%s\n", d) } } }
$ go run . i18noutterjson.i18ndad: dady != onlykey: only key != secondekey: second key != mother.daugther: fille !=
根据评论进行编辑。添加一种使用任意 json 而不是结构的方法。
func witharbitraryjson(enjson []byte, frjson []byte) { var en map[string]interface{} err := json.unmarshal(enjson, &en) if err != nil { panic(err) } var fr map[string]interface{} err = json.unmarshal(frjson, &fr) if err != nil { panic(err) } diff := deep.equal(en, fr) for _, d := range diff { if strings.hassuffix(d, "!=") { fmt.printf("%s\n", d) } } }
输出:
$ go run . map[i18n-outterJSON].map[i18n-dad]: dady !=map[onlykey]: only key != map[secondekey]: second key != map[mother]: map[daugther:fille] !=
文中关于的知识介绍,希望对你的学习有所帮助!若是受益匪浅,那就动动鼠标收藏这篇《如何通过循环 JSON 显示 golang 中丢失的键?》文章吧,也可关注golang学习网公众号了解相关技术文章。
-
502 收藏
-
502 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
139 收藏
-
204 收藏
-
325 收藏
-
477 收藏
-
486 收藏
-
439 收藏
-
357 收藏
-
352 收藏
-
101 收藏
-
440 收藏
-
212 收藏
-
143 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 508次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 484次学习