I have the keys and values as strings and i need to combine those into a json format.
For example : my "keys"
string array is : {"a","b","c","d"}
my "values"
string array i am getting through a for loop as it is stored in array X
X[0], X[1], X[2] and so on..
how do i combine "keys"
and "values"
to look like this:
{ "a":"x","b":"y", "c":"z", "d":"q"
}
I have tried iterating and combining but I'm stuck
std::string values="";
std::string keys[4]={"a","b","c","d"};
..
..
..
for(int i=0;i<4;i++)
{
values= values + "," + x[i];
}
I'm confused as to how do i combine these two strings and display a resultant string that looks like this:
{ "a":"x",
"b":"y",
"c":"z",
"d":"q"
}
Manual way can be done like:
std::string res = "{";
std::string keys[4]={"a","b","c","d"};
std::string values[4]={"a","b","c","d"};
const char* sep = "";
for(int i=0;i<4;i++)
{
res += sep + keys[i] + ":" + values[i];
sep = ",";
}
res += "}";
Using any json library, you might do something like:
Json::Value root(Json::ValueType::objectValue);
for (int i = 0; i < 4; i++) {
root[keys[i]] = values[i];
}
Json::StyledWriter writer;
writer.write(root);