尝试访问json数据时获取null C ++

时间:2019-04-29 07:15:17

标签: c++ json

从HTTP POST响应中,我检索到JSON

现在,我想在JSON中获取first parameter的值 这是一个值为1的整数

std::string postDatac = "{\"PayloadType\":1,\"TestType\":0,\"IssueDate\":\"270419\"}";

           Json::Value root;
           Json::Reader reader;
           bool parsingSuccessful = reader.parse( postDatac, root );
           if ( !parsingSuccessful )
           {
               std::cout << "Error parsing the string"  ;
           }

           const Json::Value code = root["PayloadType"];

           for ( int i = 0; i < code.size(); i++ )
           {
               // Print the values
               std::cout << code[i]  ;

       }

https://github.com/mrtazz/restclient-cpp/tree/master/vendor/jsoncpp-0.10.5/dist/json。我使用了该项目中的JSON库

当我尝试调试代码时,变量 code 始终为空,请问任何人指出我做错了什么。非常感谢。

1 个答案:

答案 0 :(得分:1)

const Json::Value code = root["PayloadType"];

code是一个intValue,可以使用code.type()找到,它返回映射到1的{​​{1}}。

enum ValueType::intValue返回code.size(),如果0不是Value,甚至不应该进入for循环(在我的情况下就是这样)。

如果您想从arrayValue中读取整数,请使用

code

应该提供输出std::cout<< code.asInt() <<std::endl;