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

如何使用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 ?

  • 写回答

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).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图