英文:
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("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!”."
答案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["emitUTF8"] = true;
std::unique_ptr<Json::StreamWriter> writer(builder.newStreamWriter())
writer->write(tempDialogue, &txtFile);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论