我正在使用JsonCpp来构建一个JSON对象.一旦构建了对象,有没有办法让对象成为一个std::string?
我有一个C++应用程序,它使用jsoncpp来解码JSON字符串.我创建了以下函数,但它只显示了顶级对象...
如何让它转储整个对象列表?
- 功能 -
SaveJSON( json_data );
bool CDriverConfigurator::PrintJSONTree( Json::Value & root, unsigned short depth /* = 0 */)
{
printf( " {type=[%d], size=%d} ", root.type(), root.size() );
if( root.size() > 0 ) {
for( Json::ValueIterator itr = root.begin() ; itr != root.end() ; itr++ ) {
PrintJSONTree( itr.key(), depth+1 );
}
return true;
}
// Print depth.
for( int tab = 0 ; tab < depth; tab++) {
printf( "-");
}
if( root.isString() ) {
printf( …Run Code Online (Sandbox Code Playgroud) JSONCPP有一个编写器,但它似乎只是从解析器获取信息,然后将其输出到字符串或流中.如何更改或创建新对象,数组,值,字符串等并将其写入文件?
我正在尝试读取JSON文件.到目前为止,我一直专注于使用该jsoncpp库.但是,我很难理解文档.任何人都可以用非专业术语解释它的作用吗?
说我有一个people.json看起来像这样的:
{"Anna" : {
"age": 18,
"profession": "student"},
"Ben" : {
"age" : "nineteen",
"profession": "mechanic"}
}
Run Code Online (Sandbox Code Playgroud)
当我读到这个时会发生什么?我可以建立某种形式的数据结构people,我可以指数Anna和Ben以及age和profession?什么是数据类型people?我认为它会类似于(嵌套)地图,但地图值总是必须具有相同的类型,不是吗?
我之前使用过python,而我的"目标"(可能是C++的错误设置)是获得等效的嵌套python字典.
我有以下代码:
void MyClass::myMethod(Json::Value& jsonValue_ref)
{
for (int i = 0; i <= m_stringList.size(); i++)
{
if (m_boolMarkerList[i])
{
jsonValue_ref.append(stringList[i]);
}
}
}
void MyClass::myOuterMethod()
{
Json::Value jsonRoot;
Json::Value jsonValue;
myMethod(jsonValue);
jsonRoot["somevalue"] = jsonValue;
Json::StyledWriter writer;
std::string out_string = writer.write(jsonRoot);
}
Run Code Online (Sandbox Code Playgroud)
如果所有boolMarkers都为false,则out_string为{"somevalue":null},但我希望它是一个空数组:{"somevalue":[]}
有谁知道如何实现这一目标?
非常感谢你!
我的代码崩溃(调试错误!R6010 abort()已被调用).你能帮助我吗?我也想知道如何从字符串值初始化json对象.
Json::Value obj;
obj["test"] = 5;
obj["testsd"] = 655;
string c = obj.asString();
Run Code Online (Sandbox Code Playgroud) 我最近使用VS71 makefile编译了JSONCPP的SVN版本.它工作,但我收到很多链接器错误
1>msvcprt.lib(MSVCP100.dll) : error LNK2005: "public: __int64 __thiscall std::basic_streambuf<char,struct std::char_traits<char> >::sputn(char const *,__int64)" (?sputn@?$basic_streambuf@DU?$char_traits@D@std@@@std@@QAE_JPBD_J@Z) already defined in JSONCPP.lib(json_writer.obj) 1>msvcprt.lib(MSVCP100.dll) : error LNK2005: "public: void __thiscall std::basic_ostream<char,struct std::char_traits<char> >::_Osfx(void)" (?_Osfx@?$basic_ostream@DU?$char_traits@D@std@@@std@@QAEXXZ) already defined in JSONCPP.lib(json_writer.obj) 1>msvcprt.lib(MSVCP100.dll) : error LNK2005: "public: class std::basic_ostream<char,struct std::char_traits<char> > * __thiscall std::basic_ios<char,struct std::char_traits<char> >::tie(void)const " (?tie@?$basic_ios@DU?$char_traits@D@std@@@std@@QBEPAV?$basic_ostream@DU?$char_traits@D@std@@@2@XZ) already defined in JSONCPP.lib(json_writer.obj) 1>msvcprt.lib(MSVCP100.dll) : error LNK2005: "protected: void __thiscall std::basic_streambuf<char,struct std::char_traits<char> >::_Init(void)" (?_Init@?$basic_streambuf@DU?$char_traits@D@std@@@std@@IAEXXZ) already defined in JSONCPP.lib(json_writer.obj) 1>msvcprt.lib(MSVCP100.dll) : error LNK2005: "protected: __thiscall std::basic_streambuf<char,struct std::char_traits<char> >::basic_streambuf<char,struct std::char_traits<char> >(void)" …
我正在使用jsoncpp,我在使用其中一个Writers编写json消息时遇到了问题.
例如:
root["name"] = "monkey";
std::cout << writer.write(root) << "\n";
Run Code Online (Sandbox Code Playgroud)
给了我这样格式的东西
{
"name" : "monkey"
}
Run Code Online (Sandbox Code Playgroud)
虽然我真的想要:
{"name":"monkey"}
Run Code Online (Sandbox Code Playgroud)
我查看了文档并提到setIndentLength()但是它们没有出现在源文件中,所以可能它们已经被弃用了.
无论如何谁都知道如何做到这一点?
我使用jsoncpp,它很棒,但当我需要检查json结构是否包含标签时,我这样做:
UserRoot0["error"].isNull()
Run Code Online (Sandbox Code Playgroud)
它从json_value.cpp第1025行抛出断言
JSON_ASSERT( type_ == nullValue || type_ == objectValue );
Run Code Online (Sandbox Code Playgroud)
我想检查我得到的响应是否来自这种类型:
{
"error" : {
"message" : "Error validating application.",
"type" : "OAuthException",
"code" : 190
}
}
Run Code Online (Sandbox Code Playgroud) 我正在使用jsoncpp从JSON文件中读取设置.
我想有两个级联设置文件,说MasterSettings.json和LocalSettings.json这里LocalSettings是MasterSettings的一个子集.我想先加载MasterSettings然后再加载LocalSettings.在LocalSettings的值与MasterSettings不同的情况下,该值将覆盖MasterSettings中的值.很像CSS中的级联.
用jsoncpp有没有优雅的方法呢?