iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > GO >如何通过循环 JSON 显示 golang 中丢失的键?
  • 184
分享到

如何通过循环 JSON 显示 golang 中丢失的键?

2024-04-05 00:04:17 184人浏览 独家记忆
摘要

编程网今天将给大家带来《如何通过循环 JSON 显示 golang 中丢失的键?》,感兴趣的朋友请继续看下去吧!以下内容将会涉及到等等知识点,如果你是正在学习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, "!= <does not have key>") {
            fmt.printf("%s\n", d)
        }
    }
}

输出:

$ go run .
map[i18n-outterJSON].map[i18n-dad]: dady != <does not have key>
map[onlykey]: only key != <does not have key>
map[secondekey]: second key != <does not have key>
map[mother]: map[daugther:fille] != <does not have key>

文中关于的知识介绍,希望对你的学习有所帮助!若是受益匪浅,那就动动鼠标收藏这篇《如何通过循环 JSON 显示 golang 中丢失的键?》文章吧,也可关注编程网公众号了解相关技术文章。

您可能感兴趣的文档:

--结束END--

本文标题: 如何通过循环 JSON 显示 golang 中丢失的键?

本文链接: https://www.lsjlt.com/news/596776.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作