2017-01-31 88 views
0

我在JSONcpp中有一个根,具有像这样的字符串值。JSONCPP正在向字符串添加额外的双引号

Json::Value root; 
std::string val = "{\"stringval\": \"mystring\"}"; 
Json::Reader reader; 
bool parsingpassed = reader.parse(val, root, false); 

现在,当我试图使用这段代码检索这个值。

Json::StreamWriterBuilder builder; 
builder.settings_["indentation"] = ""; 
std::string out = Json::writeString(builder, root["stringval"]); 

这里走出来的字符串理想应该得到含:

"mystring" 

,而它给输出这样的:顺便说一下

"\"mystring\"" \\you see this in debug mode if you check your string content 

如果打印使用stdout它将这个值被打印像这样的东西::

"mystring" \\ because \" is an escape sequence and prints " in stdout 

应该打印像这样stdout

mystring \\Expected output 

任何想法转换JSON输出时的std :: string如何避免这种产出的? 请避免建议fastwriter,因为它也增加了换行符,并且它也弃用了API。

约束:我不想通过去除多余的\”与字符串操作,而我愿意修改字符串知道我我怎么能做到这一点与JSONcpp直接

This is StreamWriterBuilder Reference code which I have used

Also found this solution, which gives optimal solution to remove extra quotes from your current string , but I don't want it to be there in first place

+0

我无法重现此输出。 –

+0

我正在使用JsonCPP 1.7.4 – spt025

+0

使用当前的1.8.0进行测试,并且仅使用1.7.4进行了测试。仍然不能。 –

回答

0

好吧所以这个问题没有得到解释后,以及我必须通过JSONCPP apis和文档一段时间回答。

我没有找到任何api作为现在需要照顾这种额外双引号加法的情况。 现在从他们的wikibook中我可以看出,一些转义序列可能以字符串形式出现。它的设计和他们没有提到确切的情况。

\" - quote 
    \\ - backslash 
    \/ - slash 
    \n - newline 
    \t - tabulation 
    \r - carriage return 
    \b - backspace 
    \f - form feed 
    \uxxxx , where x is a hexadecimal digit - any 2-byte symbol 

Link Explaining what all extra Escape Sequence might come in String

任何解决此,如果发现了同样的问题,更好的解释来了,请随意张贴您的answer.Till那么我想唯一的字符串操作是消除这些额外的转义序列的选择..

1

我也有这个问题,直到我意识到你不得不使用Json::Value类访问函数,例如root["stringval"]将为"mystring",但root["stringval"].asString()将为mystring

相关问题