我有一个file.json
编码为 KOI8-R 的 json 文件。
Boost Json 仅适用于 UTF-8 编码,因此我将文件从 KOI8-R 转换为 UTF-8:
boost::property_tree::ptree tree;
std::locale loc = boost::locale::generator().generate(ru_RU.UTF-8);
std::ifstream ifs("file.json", std::ios::binary);
ifs.imbue(loc)
boost::property_tree::read_json(ifs, tree);
但是,无法读取文件..我做错了什么?
更新:
我制作了一个 JSON 文件“test.txt”:
{
"соплодие": "лысеющий",
"обсчитавший": "перегнавший",
"кариозный": "отдёргивающийся",
"суверенен": "носившийся",
"рецидивизм": "поляризуются"
}
并将其保存在 koi8-r 中。
我有一个代码:
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
int main() {
boost::property_tree::ptree pt;
boost::property_tree::read_json("test.txt", pt);
}
编译,运行并得到以下错误:
terminate called after throwing an instance of 'boost::wrapexcept<boost::property_tree::json_parser::json_parser_error>'
what(): test.txt(2): invalid code sequence
Aborted (core dumped)
然后我使用 boost 语言环境:
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/locale/generator.hpp>
#include <boost/locale/encoding.hpp>
int main() {
std::locale loc = boost::locale::generator().generate("ru_RU.utf8");
std::ifstream ifs("test.txt", std::ios::binary);
ifs.imbue(loc);
boost::property_tree::ptree pt;
boost::property_tree::read_json(ifs, pt);
}
编译(g++ main.cpp -lboost_locale
),运行并得到以下错误:
terminate called after throwing an instance of 'boost::wrapexcept<boost::property_tree::json_parser::json_parser_error>'
what(): <unspecified file>(2): invalid code sequence
Aborted (core dumped)