2

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!!!

0

3 Answers 3

3

You have to initialize content as an array doing this:

Json::Value content(Json::arrayValue);
Sign up to request clarification or add additional context in comments.

Comments

2

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" ]
}

Comments

0

afaik content should be defined as Json::arrayValue instead of content

2 Comments

why not? if he declares variable as Json::arrayValue then his function will work properly
@stetoc your answer forced me to read a lot on meta on the topic of low quality answer and not an answer identification. While I still have my opinion that your answer could be improved, I revoked not an answer flag and admit my mistake. It's a topic discussed widely and coming back like a boomerang but the majority of experienced SO users seem to consider answers similar to yours to be valid answers. They still recommend editing and improving these answers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.