使用C ++ REST SDK(卡萨布兰卡)从文件读取JSON [英] Reading JSON from a file using C++ REST SDK (Casablanca)
本文介绍了使用C ++ REST SDK(卡萨布兰卡)从文件读取JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下代码,应读取文本文件的内容并将其解析为JSON
I have the following code which should read the contents of a text file and parse it as 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文件
And the following test JSON file
[
{
"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列语法错误:令牌格式错误".据我所知,JSON的格式正确且括号括起来.
The trouble is no matter what the JSON code I get the error 'Line 1, Column 2 Syntax error: Malformed token'. From what I can tell the JSON is correctly formatted with all brackets balanced.
代码在64位Windows 7上运行.
Code is running on 64bit Windows 7.
任何人都知道为什么会这样(或者如何将stringstream_t转换为字符串并查看其实际读取内容).
Anyone got an idea why it thinks this (or how I can convert the stringstream_t to a string and see what it actually reads).
推荐答案
更改行
v.parse(s);
到
v = json::value::parse(s)
这篇关于使用C ++ REST SDK(卡萨布兰卡)从文件读取JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文