Click here to Skip to main content
15,888,113 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi all I'm receiving a JSON array from a Web server and would like to know if there is a way using rapidjson to access field values by ordinal, the field "id" in the example code is always present but "title" can be different depending on the content

C++
for (SizeType i = 0; i < result_loop.Size(); i++)
{
    cout << result_loop[i]["id"].GetInt() << " - " << result_loop[i]["title"].GetString() << "\n";
}


sorry about the fomatting

What I have tried:

for (SizeType i = 0; i < result_loop.Size(); i++)
{
std::cout << result_loop[i]["id"].GetInt() << " - " << result_loop[i]["title"].GetString() << "\n";
}
Posted
Updated 10-May-16 5:56am
v2

1 solution

If it is not sure, that the data is in the answer data should should check before any access to avoid unwanted errors and its consequences.


C++
for (SizeType i = 0; i < result_loop.Size(); i++)
{
   var id =  result_loop[i]["id"];
   var title = result_loop[i]["title"];
    //check for validity
    if( (id != null) && (title !=null) ) {
       cout << id.GetInt() << " - " << title.GetString() << "\n";
    }
   // do some other error handling
}


Smart error handling is key to a smooth and bullet proof software. ;-)
 
Share this answer
 
Comments
pkfox 11-May-16 4:50am    
I've discovered rapidjson has a HasMember method which I think will do the trick thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900