0
我有json需要測試一個帶有utf16寬字符的字符串,但是我收到以下錯誤信息:json string with utf16 char無法從'const char [566]'轉換爲'std :: basic_string <_Elem,_Traits,_Ax>'
\..\test\TestClass.cpp(617): error C2440: 'initializing' : cannot convert from 'const char [566]' to 'std::basic_string<_Elem,_Traits,_Ax>'
with
2> [
2> _Elem=wchar_t,
2> _Traits=std::char_traits<wchar_t>,
2> _Ax=std::allocator<wchar_t>
2> ]
2> No constructor could take the source type, or constructor overload resolution was ambiguous
這是我的JSON:
static std::wstring& BAD_JSON5_missingComma_multipleNewlines_Utf16MixedDosAndUnixLineEndings()
{
static std::wstring j =
"{\n" <=VS squigly says error on this line
"\"header\":{\n"
"\"version\":{\"major\":1,\"minor\":0,\"build\":0}\n"
"},\n"
"\"body\":{\n"
"\"string\":{\"type\":\"OurWideStringClass\",\"value\":\"foo\"},\n\n\n\n"
"\"int\":[\n"
"{\"type\":\"string\",\"value\":\"\\u9CE5\"},\n"
"{\"type\":\"Int\",\"value\":5678}\n"
"],\n"
"\"double\":{\"type\":\"Double\",\"value\":12.34},\n"
"\"path1\":[\n"
"{\n"
"\"string\":{\"type\":\"OurWideStringClass\",\"value\":\"bar\"},\r\n"
"\"int\":[\n"
"{\"type\":\"Int\"\"value\":7},\n"
"{\"type\":\"Int\",\"value\":11}\n"
"]\n"
"},\n"
"{\n"
"\"string\":{\"type\":\"OurWideStringClass\",\"value\":\"top\"},\n"
"\"int\":[\n"
"{\"type\":\"Int\",\"value\":13},\r\n"
"{\"type\":\"Int\",\"value\":41}\n"
"]\n"
"}\n"
"],\n"
"\"path2\":{\n"
"\"int\":{\"type\":\"Int\",\"value\":-1234},\n"
"\"double\":{\"type\":\"Double\",\"value\":-1.234}\r\n"
"}\n"
"}\n"
"}\n"; <=double clicking build error goes to this line
return j;
}
這是如何使用它
OurWideStringClass slxStJson5 = BAD_JSON5_missingComma_multipleNewlines_Utf16MixedDosAndUnixLineEndings();
std::wistringstream ssJsonMissingCommaUtf16Newlines(slxStJson5);
我以爲我公頃d在我的json中,wchar_t覆蓋了std :: wstring。任何想法是什麼問題?你可以在\ u9ce5中看到我的utf16字符。這是這個測試的關鍵。
我看着這個c2440,但沒有看到他們在關於UDT的決議中提到的內容。
我看着這個which puts an L在它的前面,但逃脫了C字符串,我不知道放在哪裏L.
太好了!謝謝!當我做了一個json的wcout,沒有「:L \」\\ u9CE5 \「},\ n」,它沒有顯示日文字符。它顯示「\ u9CE5。這是不正確的嗎?當我添加L .....」:L \「\\ u9ce5時,json在那裏是無效的任何想法?我把L's放在像你這樣的所有行之前 – Michele
你看到我的評論嗎? – Michele
'當我做了一個JSON的wcout,沒有「:\ n」,它沒有顯示日文字符。 「\ u9CE5。這不正確嗎? - 不,這是正確的:該符號在JSON中被編碼爲Unicode序列,但它仍然代表'Han Character'bird'',任何理智的JSON解析器都將如此解釋。 –