【问题标题】:json string with utf16 char cannot convert from 'const char [566]' to 'std::basic_string<_Elem,_Traits,_Ax>'带有 utf16 char 的 json 字符串无法从 'const char [566]' 转换为 'std::basic_string<_Elem,_Traits,_Ax>'
【发布时间】:2017-04-09 16:58:50
【问题描述】:

我的 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:

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);

我以为我的 wchar_t 在我的 json 中被 std::wstring 覆盖。有什么想法有什么问题吗?您可以在 \u9ce5 中看到我的 utf16 字符。这是本次测试的关键。

我查看了这个c2440,但看不到他们在关于 UDT 的决议中指的是什么。

我在看前面的这个which puts an L ,但是用转义的c字符串,我不知道把L放在哪里。

【问题讨论】:

    标签: json utf-16 c++98


    【解决方案1】:

    std::wstring 不能像"my string" 这样从narrow string literal 初始化,因此会出现编译错误。

    您可以从 wide string literal 初始化 std::wstring — 它的语法包括在开头引号之前的 L 前缀, 例如L"my string"

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

    static std::wstring j =
        L"{\n"
            L"\"header\":{\n"
                L"\"version\":{\"major\":1,\"minor\":0,\"build\":0}\n"
            L"},\n"
        ...
    

    【讨论】:

    • 太棒了!谢谢!当我对 json 进行 wcout 时,没有 ":L\"\\u9CE5\"},\n",它没有显示日文字符。它显示“\u9CE5。这是不正确的吗?当我添加 L.....”:L\"\\u9ce5 时,json 在附近无效。有什么想法吗?我把 L 放在像你这样的所有行之前说在那里做。
    • 你看到我的评论了吗?
    • When I do a wcout of the json, without ":L\"\\u9CE5\"},\n", it's not showing the Japanese character. It's showing "\u9CE5. Is that incorrect? — 不,这是正确的:该符号在 JSON 中被编码为 Unicode 序列,但它仍然代表Han Character 'bird',任何理智的 JSON 解析器都会这样解释它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-23
    • 2014-03-02
    • 2014-07-13
    • 1970-01-01
    • 2017-03-10
    • 2022-11-14
    相关资源
    最近更新 更多