From an HTTP POST response, I retrieve a JSON

Now, I would like to get the value of the first parameter in the JSON which is an integer with a value of 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]  ;

       } I used the JSON library from this project

When I try to debug the code, the variable code keep getting null, could anyone please point out what I did wrong. Many thanks.

1 Answers

Sailesh D On Best Solutions

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

code is an intValue which can be found by using code.type() which returns 1 that is mapping to enum ValueType::intValue.

code.size() returns 0 if the Value is not arrayValue and shouldn't even enter the for loop (which is happening in my case).

If you want to read the integer from code, just use

std::cout<< code.asInt() <<std::endl;

which should give the output 1.