Jsoncpp 将一些字符识别为转义的 Unicode。

huangapple go评论76阅读模式
英文:

Jsoncpp reading some characters as escaped unicode

问题

Problem: 我尝试从JSON文件中提取一行并将其放入一个文本文件中。问题在于某些字符被表示为转义的Unicode,而不是标准的Unicode表示。

Attempts to fix this issue: 我不确定是什么导致了这个问题。我猜测与JSONcpp使用的Unicode 8有关,而不是Unicode 16。我查找了一种将Unicode 8字符串转换为16的方法,参考了https://stackoverflow.com/questions/4804298/how-to-convert-wstring-into-string。但那也没有起作用。我还查找了文件流系统是否有问题,参考了https://en.cppreference.com/w/cpp/io/basic_fstream,但也没有得到我想要的结果。

example code...

    Json::Value root;
    Json::Reader reader; 
    Json::StyledStreamWriter writer;
    ifstream  file("fileName");
    //wifstream  file(L"fileName");
    //ifstream<wchar_t> file("FileName");
    std::fstream txtFile("txtFileName");
    reader.parse(file, root);
    Json::Value events = root["events"];

//contains "He said “stop!”."
tempDialogue = events[eventIndex]["pages"][pageIndex]["list"][listIndex]["parameters"][0];
writer.write(txtFile, tempDialogue);
file.close();
txtFile.close();

text File than contains
"He said \u201cHey stop \u201d."
expected result
"He said “stop!”."

英文:

Problem: I'm trying to extract a line from a json file and put it in a txt file. The issue is that certain characters are represented as escaped unicode instead of the standard unicode representation.

Attempts to fix this issue: I'm not exactly sure what is causing this issue. My guess is that it has something to do with JSONcpp being unicode 8 instead of unicode 16. I looked up on ways to convert a unicode 8 string to 16 by looking up https://stackoverflow.com/questions/4804298/how-to-convert-wstring-into-string. But that didn't work either. I also looked up if it was something with the file stream system https://en.cppreference.com/w/cpp/io/basic_fstream and that did not get me my desired results either.
](https://stackoverflow.com)
example code...

    Json::Value root;
    Json::Reader reader; 
    Json::StyledStreamWriter writer;
    ifstream  file(&quot;fileName&quot;);
    //wifstream  file(L&quot;fileName&quot;);
    //ifstream&lt;wchar_t&gt; file(&quot;FileName&quot;);
    std::fstream txtFile(&quot;txtFileName&quot;);
    reader.parse(file, root);
    Json::Value events = root[&quot;events&quot;];

//contains &quot;He said “stop!”.&quot;
tempDialogue = events[eventIndex][&quot;pages&quot;][pageIndex][&quot;list&quot;][listIndex][&quot;parameters&quot;][0];
writer.write(txtFile, tempDialogue);
file.close();
txtFile.close();

text File than contains
"He said \u201cHey stop \u201d."
expected result
"He said “stop!”."

答案1

得分: 0

JSON规范允许以本地UTF-8编码或\uXXXX转义形式表示非ASCII字符。
JSONCPP在内部将所有内容转换为本地UTF-8编码,所以您所看到的是默认配置下StreamWriter的结果。

您可以自行构建一个只发出UTF-8形式的StreamWriter,使用一个StreamWriterBuilder:

StreamWriterBuilder builder;
builder["emitUTF8"] = true;
std::unique_ptr<Json::StreamWriter> writer(builder.newStreamWriter());
writer->write(tempDialogue, &txtFile);
英文:

The JSON specification allows non-ascii characters to be represented in the native UTF-8 encoding or as a \uXXXX escape.
JSONCPP converts everything into the native UTF-8 encoding internally, so what you are seeing is result of the default configuration of the StreamWriter.

You can construct a StreamWriter yourself that emits only the UTF-8 form using a StreamWriterBuilder:

StreamWriterBuilder builder;
builder[&quot;emitUTF8&quot;] = true;
std::unique_ptr&lt;Json::StreamWriter&gt; writer(builder.newStreamWriter())
writer-&gt;write(tempDialogue, &amp;txtFile);

huangapple
  • 本文由 发表于 2023年6月29日 20:59:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76581309.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定