Hi,
this is my first time I’m using rapidjson.
I successfully parse the following rapidjason
{
“hello”: “world”,
“t”: true ,
“f”: false,
“n”: null,
“i”: 123,
“pi”: 3.1416,
“a”: [1, 2, 3, 4]
}
But I have problem in parsing json object.
I have following json string
{“employees”:[
{“firstName”:“John”, “lastName”:“Doe”},
{“firstName”:“Anna”, “lastName”:“Smith”},
{“firstName”:“Peter”, “lastName”:“Jones”}
]}
please tell me how to parse it.
Thanks
Hello @NileshShripal try this,
std::string data = {"employees":[
{"firstName":"John", "lastName":"Doe"},
{"firstName":"Anna", "lastName":"Smith"},
{"firstName":"Peter", "lastName":"Jones"}
]
}
rapidjson::Document document;
if(!parseJsonData(data, document)){
// parse error
log("Parse Error Check format of Json, or response");
return ;
}
if(document.IsObject()){
if(document.HasMember("employees")){
const rapidjson::Value& employeeArray = document["employees"]; // you are missing this
assert(employeeArray.IsArray());
for (rapidjson::SizeType i = 0; i < employeeArray.Size(); i++)
{
const rapidjson::Value & atomicObject = employeeArray[i];
std::string firstName;
std::string lastName;
if(atomicObject.HasMember("firstName")){
const rapidjson::Value & name = atomicObject["firstName"];
firstName = cocos2d::Value(name.GetString()).asString();
}
if(atomicObject.HasMember("lastName")){
const rapidjson::Value & name = atomicObject["lastName"];
lastName = cocos2d::Value(name.GetString()).asString();
}
log("Full Name is :--- %s %s",firstName.c_str(), lastName.c_str());
}
}
}
where data is the data retrieved by API callback.
Regards
Ajay
Lazy_Gamer:
std::string data = {“employees”:[
{“firstName”:“John”, “lastName”:“Doe”},
{“firstName”:“Anna”, “lastName”:“Smith”},
{“firstName”:“Peter”, “lastName”:“Jones”}
]
}
rapidjson::Document document;
if(!parseJsonData(data, document)){
// parse error
log(“Parse Error Check format of Json, or response”);
return ;
}
if(document.IsObject()){
if(document.HasMember("employees")){
const rapidjson::Value& employeeArray = document["employees"]; // you are missing this
assert(employeeArray.IsArray());
for (rapidjson::SizeType i = 0; i < employeeArray.Size(); i++)
{
const rapidjson::Value & atomicObject = employeeArray[i];
std::string firstName;
std::string lastName;
if(atomicObject.HasMember("firstName")){
const rapidjson::Value & name = atomicObject["firstName"];
firstName = cocos2d::Value(name.GetString()).asString();
}
if(atomicObject.HasMember("lastName")){
const rapidjson::Value & name = atomicObject["lastName"];
lastName = cocos2d::Value(name.GetString()).asString();
}
log("Full Name is :--- %s %s",firstName.c_str(), lastName.c_str());
}
}
}
Hi Lazy_Gamer ,
I’m getting the following error:-
Assertion failed: (employeeArray.IsArray()), function onHttpRequestCompleted,
Assertion is failing make sure the response json is in correct format, check online there are many online jsonformatters available. Or if you have the API available publicly you can paste it here so that i can test it aswel
Lazy_Gamer:
jsonformatters
Thanks Problem solved. You are correct.
Thanks
@Lazy_Gamer
Hi, im trying to parse json data using rapid miner too in cocos2dx. Im stuck at here. Really dont know whats going on. Hope u could help me please
Ive imported the libraries as following:
#include “external/rapidjson/reader.h”
#include “external/rapidjson/writer.h”
#include “external/rapidjson/document.h”
#include “external/rapidjson/stringbuffer.h”
Error: No matching member function for call to ‘Parse’
Hello @karatekun ,
Your imports looks good. Include#include "json/stringbuffer.h" aswell, I am using cocos2d-X v 3.10 as of now for our game.
////////////// Parsing Json Data into a document
bool MyClient::parseJsonData(string data, rapidjson::Document& document){
const char * jsonText = data.c_str();
document.Parse<0>(jsonText);
if (document.HasParseError()) {
return false;
}
return true;
}
hope this helps you.
1 Like
@Lazy_Gamer It works Tq so much. I was feeding the function the wrong data this whole time Silly me