允许字符串数组的C++JSON编写器

C++ JSON writer that allows array of strings

本文关键字:C++JSON 字符串 数组      更新时间:2023-10-16

我需要一个C++库(或头,或代码片段),它将允许我编写包含字符串数组的JSON,例如一堆错误消息。

据我所知,Boost的属性树不允许字符串数组,尽管我在项目的另一部分使用Boost。

理想情况下,JSON可以发送到stdout,但如果必须将其写入文件,我可以接受它。

有什么想法我可以用吗?

我喜欢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);

示例取自http://www.thomaswhitton.com/blog/2013/06/27/json-c-plus-plus-examples/