c++ - 使用 C++ REST SDK (Casablanca) 从文件中读取 JSON

标签 c++ json casablanca

我有以下代码应该读取文本文件的内容并将其解析为 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/

相关文章:

c++ - 如何在 CUDA 中将密集 vector 转换为稀疏 vector ?

objective-c - NSJSONSerialization 和 NSNull

c++ - 复制和粘贴 .so 文件不适用于链接器

c++ - 添加卡萨布兰卡依赖 VS2017

C++属性未声明的变量

c++ - C++中的一种反射

c++ - 从文件中读短

java - jackson 序列号 : Unwrap collection elements using

javascript - json 响应中的日期格式不起作用

c++ - cmake 包含和库的路径问题