2

I need a C++ library (or headers, or code snippet) that will allow me to write JSON that includes an array of strings, such as a bunch of error messages.

Boost's property tree does not allow arrays of strings as far as I can tell - though I am using boost for another part of the project.

Ideally the JSON can be sent to stdout, though if it has to be written to a file I can live with that.

Any ideas what I can use?

1 Answer 1

1

I like http://jsoncpp.sourceforge.net/

Json::Value fromScratch;
Json::Value array;  // this is the array of strings
array.append("hello");
array.append("world");
fromScratch["hello"] = "world";
fromScratch["number"] = 2;
fromScratch["array"] = array;
fromScratch["object"]["hello"] = "world";

// write in a nice readible way
Json::StyledWriter styledWriter;
std::cout << styledWriter.write(fromScratch);

Example taken from http://www.thomaswhitton.com/blog/2013/06/27/json-c-plus-plus-examples/

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, JsonCpp is great!

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.