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

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

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥15 clion的参数提示怎么关闭
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻