Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preparing C++ Json Request

I am trying to prepare json request using c++. Like this:

string key = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";

std::string data = "{\n"
"    \"foo\": key\n"
"}";

When I print this, it's shows like :

"foo": key

But I need like this :

"foo": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"

So, please someone help me. How to do this? Thanks in advance.

like image 721
Jayesh Avatar asked Sep 21 '26 07:09

Jayesh


1 Answers

C++ doesn't expand variables inside a string constant, so if you have "key" inside such a string, it'll just be interpreted as the string "key" rather than expanded as a variable.

What you want to do is concatenate the contents of the variable "key" with the rest of your string. In C++, you can just do this with the concatenation operator "+".

So you'll want something like:

std::string data = "{\n"
"    \"foo\": " + key + "\n"
"}"
like image 151
simplicio Avatar answered Sep 23 '26 21:09

simplicio



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!