Label doesn't print value received from server as json

Label doesn't print value received from server as json
0.0 0

#1

Hi,

I am getting json data from server like this -

 if (response->isSucceed()) {
        log("Get success");
        std::vector<char>* buffer = response->getResponseData();
        std::string res;
        res.insert(res.begin(), buffer->begin(), buffer->end());
        log("res: %s", res.c_str());
        rapidjson::Document d;
        d.Parse<0>(res.c_str());
        if(d.IsObject()){
            
            log("true"); // print the value of obtaining hello
        }
        
         F = d["F"].GetString();
         W = d["W"].GetString();
         T = d["T"].GetString();
         Fp = d["Fp"].GetString();
         Wp = d["Wp"].GetString();
         Tp = d["Tp"].GetString();
        
}

Now i want to display that value of “F” using label…Value of F is integer number.

So i do this -

foodnumber = Label::createWithTTF(F, "fonts/A little sunshine.ttf",  40 );
foodnumber->setColor(Color3B(109,61,23));

foodnumber->setPosition( Vec2(foodtitle->getPositionX() + foodtitle->getContentSize().width/4  + origin.x, foodtitle->getPositionY() + origin.y) );
//title->setDimensions(1, 0);
this->addChild( foodnumber);

it doesn’t print the value…

The value in the F is Number, so i also tried - F = d["F"].GetInt(); - and it gives an error…

But when i log the value, it prints it right, but it doesn’t get displayed on label…

Any help would be appreciated?


#2

Did you try first removing SPACE in ttf name?
We had problem with that previously in our game.
Also try putting static string and see if its displaying properly or not.


#3

Yeah font load properly and even static string, as well as string variables…

Edit - Am i parsing the json correctly? Is there a different way to parse INT received as Json?

Also when i log the value like this, it logs the value properly -

log("String- %s", F.c_str());

Output  - String - 1

So why is it not showing in a label.


#4

Try this:

Document doc;
if (!doc.Parse(_jsonData.c_str()).HasParseError())
{
    rapidjson::Value::ConstMemberIterator dataF = doc.FindMember("dataF");
    if (dataF != doc.MemberEnd())
    {
       std::string F = dataF->value.GetString();
    }
}

#5

Hi thanks for replying…

when i use this code, it doesn’t log the value and so not on label as well…

    log("F : %s", F.c_str());

I try to log like this, but it doesn’t log value…


#6

When you got respoonseData from server, try this code to parse into rapidjson.

std::vector<char> *buffer = response->getResponseData();
unsigned char *data = (unsigned char *)&(buffer->front());

std::string jsonDataString(reinterpret_cast<char*>(data), buffer->size());
log("data : %s", jsonDataString.c_str());

rapidjson::Document doc;
doc.Parse<0>(jsonDataString.c_str());

#7

change “dataF” to your json variable name


#8

Hi guys…

Finally got the label to display the value…What is happening is, when i put the label inside this braces

 if (response->isSucceed()) {

//parsing json here
F = d["F"].GetString();
//when i put label here, it displays valaue.

}
// when i put label here, it doesn't display value.

it displays the value, and when i put it outside that, it doesn’t doesn’t display the value.

Although variable std::string F i have declared inside .h file.

The variable should be able to display the value anywhere in the .cpp file right?