rapid JSON fails with Assertion `IsObject()' failed
Your JSON string is array. So if you check IsObject()
it failed. Take a careful look at the JSON string, you will find that what you care is in []
, which show that it is an array.
Take from JSON official site:
An array begins with [ (left bracket) and ends with ] (right bracket). Values are separated by , (comma).
Try the following code:
string str_json = "[{\"Node\":\"9478149a08f9\",\"Address\":\"172.17.0.2\",\"ServiceID\":\"HSS\",\"ServiceName\":\"HSS\",\"ServiceTags\":[],\"ServiceAddress\":\"\",\"ServicePort\":6666,\"ServiceEnableTagOverride\":false,\"CreateIndex\":2855,\"ModifyIndex\":2855}]";rapidjson::Document doc;doc.Parse(str_json.c_str());//assert(doc.IsObject());if(doc.IsArray()){ cout << "is array" << endl;}for(Value::ConstValueIterator itr = doc.Begin(); itr != doc.End(); ++itr){ const Value& obj = *itr; for(Value::ConstMemberIterator it = obj.MemberBegin(); it != obj.MemberEnd(); ++it){ if(it->value.IsString()){ cout << it->name.GetString() << ": " << it->value.GetString() << endl; } // other codes... }}