doucou1892 2016-02-05 10:48
浏览 12
已采纳

从命令行嵌套的JSON运行Go

I can think of workarounds on how to get this working however I'm interested in finding out if there's a solution to this specific problem.

I've got a go program which requires a json string arguement:

go run main.go "{ \"field\" : \"value\" }"

No problems so far. However, am I able to run from the command line if one of the json values is another json string?

go run main.go "{ \"json-string\" : \"{\"nestedfield\" : \"nestedvalue\"}\" }"

It would seem that adding escape characters incorrectly matches up the opening and closing quotes. Am I minuderstanding how this is done or is it (and this is the side I'm coming down on) simply not possible?

To reiterate, this is a question that has piqued my curiosity - I'm aware of alternative approaches - I'm hoping for input related to this specific problem.

  • 写回答

2条回答 默认 最新

  • 普通网友 2016-02-05 13:51
    关注

    Based on the feedback from wiredeye I went down the argument route instead. I've modified the program to run on:

    go run main.go field:value field2:value json-string:"{\"nestedfield\":nestedvalue}"
    

    I can then iterate over the os.Args and get the nested json within my program. I'm not using flags directly as I don't know the amount of inputs into the program which would have required me to use duplicate flags (not supported) or parse the flag to a collection (doesn't seem to be supported).

    Thanks wiredeye

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么