简体   繁体   English

在 Unix 中将 json 作为命令行参数传递

[英]Passing json as command line argument in Unix

I am trying to pass a json string as command line argument to my c++ application in a Unix environment.我正在尝试在 Unix 环境中将 json 字符串作为命令行参数传递给我的 c++ 应用程序。

.\SampleApp -j {\"speed\":\"15\",\"rpm\":\"100\",\"loc\":[\"-83.11\",\"42.11\"]}

I'm using getopt() function in my sample app to parse the arguments.我在我的示例应用程序中使用 getopt() function 来解析 arguments。 At the output I am receiving only speed:15.在 output 我只收到速度:15。 But when I run the application as但是当我运行应用程序时

.\SampleApp -j \"speed\":\"15\",\"rpm\":\"100\",\"loc\":[\"-83.11\",\"42.11\"]

it works.有用。 My question is how do I pass the json string with curly braces properly to the application.我的问题是如何将带有花括号的 json 字符串正确传递给应用程序。 I tried using escape sequence, \{ , but it did not work.我尝试使用转义序列\{ ,但它不起作用。

Usually, it easier to use single quote when the text has double quotes通常,当文本有双引号时,使用单引号会更容易

./SampleApp -j '{"speed":"15","rpm":"100","loc":["-83.11","42.11"]}'

Or over multiple lines for readability:或多行以提高可读性:

./SampleApp -j '
{
    "speed":"15",
    "rpm":"100",
    "loc":["-83.11","42.11"]
}'

You can do this by just passing the argument inside " . Also, it's ./ , not .\ .您可以通过在"中传递参数来做到这一点。此外,它是./ ,而不是.\

./SampleApp -j "{\"speed\":\"15\",\"rpm\":\"100\",\"loc\":[\"-83.11\",\"42.11\"]}"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM