douya2006
2019-03-05 15:43
浏览 55
已采纳

如何使用Go编程语言读取JSON文件?

I'm working on a translation project on my Angular app. I already create all the different keys for that. I try now to use Go Programming Language to add some functionalities in my translation, to work quickly after.

I try to code a function in Go Programming Language in order to read an input user on the command line. I need to read this input file in order to know if there is missing key inside. This input user must be a JSON file. I have a problem with this function, is blocked at functions.Check(err), in order to debug my function I displayed the different variable with fmt.Printf(variable to display). I call this function readInput() in my main function.

The readInput() function is the following :

    // this function is used to read the user's input on the command line
func readInput() string {
    // we create a reader
    reader := bufio.NewReader(os.Stdin)
    // we read the user's input
    answer, err := reader.ReadString('
')
    // we check if any errors have occured while reading
    functions.Check(err)
    // we trim the "
" from the answer to only keep the string input by the user
    answer = strings.Trim(answer, "
")
    return answer
}

In my main function I call readInput() for a specific command I created. This command line is usefull to update a JSON file and add a missing key automatically.

My func main is :

      func main() { 
        if os.Args[1] == "update-json-from-json" {

    fmt.Printf("please enter the name of the json file that will be used to 
    update the json file:") 
    jsonFile := readInput()

    fmt.Printf("please enter the ISO code of the locale for which you want to update the json file: ")
            // we read the user's input
            locale := readInput()
            // we launch the script
            scripts.AddMissingKeysToJsonFromJson(jsonFile, locale)
        }

I can give you the command line I use for this code go run mis-t.go update-json-from-json

Do you what I'm missing in my code please ?

图片转代码服务由CSDN问答提供 功能建议

我正在Angular应用程序上从事翻译项目。 我已经为此创建了所有不同的键。 我现在尝试使用Go编程语言在我的翻译中添加一些功能,以便在以后快速工作。

我尝试用Go编程语言对函数进行编码,以便在Windows上读取输入用户 命令行。 我需要阅读此输入文件,以了解内部是否缺少键。 此输入用户必须是JSON文件。 我对此功能有问题,在 functions.Check(err)处被阻止,为了调试我的功能,我用 fmt.Printf(显示的变量)显示了另一个变量。 我在主函数中将此函数称为 readInput()

readInput()函数如下:

  //此函数用于在命令行上读取用户的输入
func readInput()字符串{
 //我们创建了一个读取器
读取器:= bufio  .NewReader(os.Stdin)
 //我们读取用户输入的答案
 err:= reader.ReadString('
')
 //我们检查在读取函数时是否发生任何错误。  (err)
 //我们将答案中的“ 
”修剪为仅保留用户输入的字符串
 answer = strings.Trim(answer,“ 
”)
返回答案
} 
    
 
 

在我的主要功能中,我针对创建的特定命令调用 readInput()。 此命令行对于更新JSON文件和自动添加丢失的密钥非常有用。

我的 func main 是:

  func main(){
,如果os.Args [1] ==“ update-json-from-json” {
 
 fmt.Printf(“请输入将要保存的json文件的名称 用于
更新json文件:“)
 jsonFile:= readInput()
 
 fmt.Printf(”请输入要为其更新json文件的语言环境的ISO代码:“)
  // //读取用户的输入
语言环境:= readInput()
 //启动脚本
脚本.AddMissingKeysToJsonFromJson(jsonFile,locale)
} 
   
 
  

我可以为您提供用于此代码的命令行运行mis-t.go update-json-from-json

我的代码中缺少我吗?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongou5100 2019-03-05 16:06
    已采纳

    Presuming that the file contains dynamic and unknown keys and values, and you cannot model them in your application. Then you can do something like:

    
    func main() {
        if os.Args[1] == "update-json-from-json" {
            ...
            jsonFile := readInput()
    
            var jsonKeys interface{}
            err := json.Unmarshal(jsonFile, &jsonKeys)
            functions.Check(err)
    
    
            ...
        }
    }
    

    to load the contents into the empty interface, and then use the go reflection library (https://golang.org/pkg/reflect/) to iterate over the fields, find their names and values and update them according to your needs.

    The alternative is to Unmarshal into a map[string]string, but that won't cope very well with nested JSON, whereas this might (but I haven't tested it).

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题