Rapidjson Assertion failed: (IsObject()) error

Hello i am getting this error in json parsing…

Assertion failed: (IsObject()), function FindMember, file /Users/mihir/Desktop/test/proj.ios_mac/…/cocos2d/external/json/document.h, line 620.

My code is :

void HelloWorld::onHttpRequestCompleted(HttpClient *sender,HttpResponse *response) {
  
if (response->isSucceed()) {
    log("Get success");
    std::vector<char>* buffer = response->getResponseData();
    std::string res;
    res.insert(res.begin(), buffer->begin(), buffer->end());
    rapidjson::Document d;
    d.Parse<0>(res.c_str());
    log("my id %s\n", d["id"].GetString());
}
}

I am making request to server and i get this data : id:user1 pass:thkrnull

so how to solve this error…

and why do i get null in the end, the password is only: “thkr”

Hi, here are some tips to debug your problem.

The best way is to have son document {}. Try to send from your server this:

{
"id":"user1",
"pass":"thkr"
}

Try to print content from std::string res, must be same as your server sent.

Check for any possible error during parsing in your res data.

if (false == doc.Parse <0>(res.c_str())).HasParseError()) {
// document is ok
}else{
// error
}

Hi @mihir77

I faced this issue.
you can check this link

json assertion fail link

I checked it out and it solved my issue.

Happy Coding :smile:

Thanks guys, solved the issue, the json format sent from server was wrong, now i am able to receive data.

But while posting data to server,

My code is -

HttpRequest* request = new HttpRequest();
request->setUrl("http://server/login.php");
request->setRequestType(HttpRequest::Type::POST);
std::vector<std::string> headers;
headers.push_back("Content-Type: application/json; charset=utf-8");
request->setHeaders(headers);
request->setResponseCallback(CC_CALLBACK_2(HelloWorld::onHttpRequestCompleted, this));

// write the post data

std::string postData = "test data";
request->setRequestData(postData.c_str(), postData.length());
request->setTag("POST test2");
HttpClient::getInstance()->send(request);
request->release();

here, instead of string how to send array of string, like username and password in json format to server…