Search code examples
c++arraysjsoncpp

jsoncpp write json array with only one element


I have a problem in using jsoncpp. Code is below:

Json::Value content;
for ( int i=0; i < len; ++i)
{
    content["res"].append(strs[i]);
}

My case is, when my string vector strs only have one element, the final json string are like:

"res":"a"

However, I'm expecting something like:

"res":["a"]

I hope anyone pull me out, thanks guys!!!


Solution

  • Problem Solved! Thanks for the help from both @dani2442 and @stetoc

    I run a little test with code below:

    Json::Value root, content(Json::arrayValue);
    content.append("a");
    root["res"]=content;
    cout << root.toStyledString() <<endl;
    

    the result is :

    {
        "res" : [ "a" ]
    }