我有以下代码应该读取文本文件的内容并将其解析为 JSON
try {
string_t importFile = argv[++iArgCounter]; // extract filename
ifstream_t f(importFile); // filestream of working file
stringstream_t s; // string stream for holding JSON read from file
json::value v; // JSON read from input file
iArgCounter++; // increment arg counter
if (f) {
s << f.rdbuf(); // stream results of reading from file stream into string stream
f.close(); // close the filestream
v.parse(s); // parse the resultant string stream.
}
}
catch (web::json::json_exception excep) {
std::cout << "ERROR Parsing JSON: ";
std::cout << excep.what();
break;
}
和下面的测试JSON文件
[
{
"Destinations":
[
{
"Domain": "127.0.0.1",
"Name": "GoogleLogin",
"Port": "8090"
}
],
"Listeners":
[
{
"Domain": "127.0.0.1",
"Name": "LoginRequest",
"Port": "8080",
"Route": "ProcessLoginRequest"
}
],
"Name": "LoginProcess",
"Routes":
[
{
"Name": "ProcessLoginRequest",
"Rules":
[{
"DestinationIfTrue": "GoogleLogin",
"LeftTerm":
{
"RuleTermType": 1,
"Value": "NETWORK"
},
"Operator": 2,
"RightTerm":
{
"RuleTermType": 0,
"Value": "NETWORK"
}
}],
"Transformations": []
}
]
}
]
问题是无论 JSON 代码是什么,我都会收到错误“第 1 行,第 2 列语法错误: token 格式错误”。据我所知,JSON 格式正确,所有括号均已平衡。
代码在 64 位 Windows 7 上运行。
任何人都知道它为什么这么想(或者我如何将 stringstream_t 转换为字符串并查看它实际读取的内容)。
最佳答案
换行
v.parse(s);
到
v = json::value::parse(s)
关于c++ - 使用 C++ REST SDK (Casablanca) 从文件中读取 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28972751/