C++ cpprestsdk:无法分析json输入

C++ cpprestsdk:无法分析json输入,c++,json,serialization,cpprest-sdk,C++,Json,Serialization,Cpprest Sdk,由于某种原因,fromJson方法中的For循环无法迭代可用的JSON字段。按照我的理解(基于调试),它无法将输入字符串作为JSON对象读取。但是当我在调试器中检查它时,我看到了正确的JSON字符串。 完全相同的代码适用于不同的POD类 对象: struct Config { Config(); Config(Config&&) = default; Config& operator=(Config&&) = default;

由于某种原因,
fromJson
方法中的
For
循环无法迭代可用的JSON字段。按照我的理解(基于调试),它无法将输入字符串作为JSON对象读取。但是当我在调试器中检查它时,我看到了正确的JSON字符串。 完全相同的代码适用于不同的POD类

对象:

struct Config
{
    Config();
    Config(Config&&) = default;
    Config& operator=(Config&&) = default;

    bool operator==(const Config& c) const;
    bool operator!=(const Config& c) const;

    bool isValid() const;

    unsigned short m_taskTimer;
    bool m_test;
};
序列化/反序列化
fromJson
无法解析输入字符串:

bool Serialization::fromJson(Controller::Config& c, const std::string& str)
try
{
    json::value temp;
    unsigned short count = 0;
    temp.parse(str);

    for (auto it = temp.as_object().cbegin(); it != temp.as_object().cend(); ++it)
    {
        const std::string& key = it->first;
        const json::value& value = it->second;

        if (key == "taskTimer")
        {
            c.m_taskTimer = value.as_integer();
            ++count;
            continue;
        }

        if (key == "test")
        {
            c.m_test = value.as_bool();
            ++count;
            continue;
        }

        return false;
    }

    if (count != 2)
    {
        return false;
    }

    return true;
}
catch (...)
{
    return false;
}

std::string Serialization::toJson(const Controller::Config& c)
{
    json::value temp;
    temp["taskTimer"] = json::value::number(c.m_taskTimer);
    temp["test"] = json::value::boolean(c.m_test);

    return temp.serialize();
}
测试:

我想理解的是,为什么它不能遍历字段?
temp.parse(str)
行起作用,因此库认为输入有效。

temp.parse(str)
更改为
auto-temp=json::value::parse(str)
修复了该问题

Controller::Config cc1, cc2;
cc1.m_taskTimer = 300;
cc1.m_test = true;
Serialization::fromJson(cc2, Serialization::toJson(cc1));
assert(cc1 == cc2);