带有utf16字符的json字符串无法从“const char[566]”转换为“std::basic_string”

带有utf16字符的json字符串无法从“const char[566]”转换为“std::basic_string”,json,utf-16,c++98,Json,Utf 16,C++98,我有一个json,它需要测试一个包含utf16宽字符的字符串,但我得到以下错误消息: \..\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

我有一个json,它需要测试一个包含utf16宽字符的字符串,但我得到以下错误消息:

\..\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中包含了std::wstring。你知道怎么回事吗?您可以在\u9ce5中看到我的utf16字符。这是这次测试的关键

我看了这一点,但没有看到他们在关于UDT的决议中提到了什么

我在前面看到了这个,但是对于转义的c字符串,我不确定将L放在哪里。

std::wstring不能从类似于我的字符串的字符串初始化,因此编译错误

您可以从a初始化std::wstring-其语法包括开头引号前的L前缀,例如Lmy字符串

在使用字符串文字连接时,需要在所有字符串文字前面加上L,即:


伟大的谢谢当我对json执行wcout时,没有:L\\\u9CE5\},\n,它不会显示日文字符。它正在显示\u9CE5。这不对吗?当我添加L…:L\\\u9ce5,此处附近的json无效。有什么想法吗?我把L放在你说的所有行之前。你看到我的评论了吗?当我用json做一个wcout时,没有:L\\\u9CE5\},\n,它没有显示日文字符。它正在显示\u9CE5。不对吗不,这是正确的:符号在JSON中被编码为Unicode序列,但它仍然表示汉字“bird”,任何理智的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);
static std::wstring j =
    L"{\n"
        L"\"header\":{\n"
            L"\"version\":{\"major\":1,\"minor\":0,\"build\":0}\n"
        L"},\n"
    ...