@FubiraiHan

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

[c++]nlohmannで日本語を含む文字列をパースするとエラーが出る

Q&A

以下のコードを実行するとエラーが出ます。

#include <nlohmann/json.hpp>

using json = nlohmann::json;

int main() {
    json j = json::parse("{\"myname\":\"あああ\"}");
    return 0;
}

エラー:

0x00007FF84DD57F9A で例外がスローされました (test.exe 内): Microsoft C++ の例外: nlohmann::json_abi_v3_12_0::detail::parse_error (メモリの場所 0x0000003106F1CA88)。
ハンドルされない例外が 0x00007FF84DD57F9A で発生しました (test.exe 内): Microsoft C++ の例外: nlohmann::json_abi_v3_12_0::detail::parse_error (メモリの場所 0x0000003106F1CA88)。

日本語を値として入れている部分が問題のようで、アルファベットに置き換えるとエラーなく実行できます。
nlohmannの内部では文字列データをutf-8で保存しているみたいなので日本語もいけそうな気がするのですが、エラーをなくすにはどうすればいいでしょうか。

0 likes

1Answer

Only UTF-8 encoded input is supported

なので文字列リテラルの前に "u8" のプレフィックスをつけてください

json j = json::parse(u8"{\"myname\":\"あああ\"}");
0Like

Your answer might help someone💌