【问题标题】:Reading JSON from a file using C++ REST SDK (Casablanca)使用 C++ REST SDK (Casablanca) 从文件中读取 JSON
【发布时间】:2015-03-10 19:45:18
【问题描述】:

我有以下代码应该读取文本文件的内容并将其解析为 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 列语法错误:格式错误的令牌”。据我所知,JSON 格式正确,所有括号均平衡。

代码在 64 位 Windows 7 上运行。

任何人都知道为什么会这样认为(或者我如何将 stringstream_t 转换为字符串并查看它实际读取的内容)。

【问题讨论】:

  • 我面临同样的问题。我正在努力解决这个问题。任何使用 cpprestsdk(casablanca) 的 c++ 开发人员请帮助我。因为在文档中没有看到任何内容。
  • 你在同一个域上工作

标签: c++ json casablanca


【解决方案1】:

换行

v.parse(s);

 v = json::value::parse(s)

【讨论】:

    【解决方案2】:
    • 会不会是文件是utf16编码的?
    • 或使用十六进制编辑器打开 json 文件,检查其头部是否有 BOM(字节 Oder 标记)。

    【讨论】:

      最近更新 更多