简体   繁体   中英

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.

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

I'm using getopt() function in my sample app to parse the arguments. At the output I am receiving only speed: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. 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\"]}"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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