JSON Parsing in Cocos2Dx v3.4

I need to read an external json file.
I’ve found a lot of old examples but I think that now the situation is changed.

In API reference I’ve found a JSON Class but don’t know how to use.

I’ve found this working example in chinese:
http://cocos2d-x.org/docs/manual/framework/native/v3/json-parse/zh

It works with rapidjson external lib, my question is: in 3.4 is there something internal or I have to use rapidjson or other?

Thank you

Yes, you use rapidjson if you want to use what is part of the cocos2d-x “standard.”

Here is an example of how to parse a JSON string:

profileInfo Parser::parseProfile(string json)
{
    auto profile = *new profileInfo();
    Document document;
    document.Parse<0>(json.c_str());
    
    if (document.HasMember("id")){
        profile.id = document["id"].GetString();
        profile.name = document["name"].GetString();
        
        vector<levelInfo> levels = *new vector<levelInfo>;
        for (SizeType i = 0; i < document["levels"].Size(); i++){
            auto level = *new levelInfo;
            level.num = document["levels"][i]["num"].GetInt();
            
            vector<activityInfo> activities = *new vector<activityInfo>;
            for (SizeType j = 0; j < document["levels"][i]["activities"].Size(); j++){
                auto activity = *new activityInfo;
                activity.num = document["levels"][i]["activities"][j]["num"].GetInt();
                activity.score = document["levels"][i]["activities"][j]["score"].GetInt();
                activity.bonus = document["levels"][i]["activities"][j]["bonus"].GetInt();
                activity.played = document["levels"][i]["activities"][j]["played"].GetBool();
                activities.push_back(activity);
            }
            
            level.activities = activities;
            levels.push_back(level);
        }
        profile.levels = levels;
    }
    
    return profile;
}

I hope this helps get you started!

2 Likes

Perfect and clear, thank you!

Hey,

Can you elaborate for me(a beginner) as in how to use it to parse my JSON file.
I mean suppose I put this code as my parser.cpp and then how shall I use this thing to pass my file.json as an argument ?

Thanxx :smile:

No problem.

@code_game_chef If your json is in a file and you want to open it and parse it you can do something like this:

    long filesize = 0;
    string content;
    string fullPath = "res/levels.json";
    
    auto fileData = FileUtils::getInstance()->getFileData(fullPath.c_str(), "r", &filesize);
    content.append((char*)fileData);
    delete[] fileData;
    
    // this would call your own Parser class based on my example above:
    gameInfo = Parser::parseGameData(content);

I hope that helps!

1 Like

Here is an updated version as I found problems with the getFileData method in FileUtils.

string fullPath = "res/levels.json";
        
auto fileData = FileUtils::getInstance()->getDataFromFile(fullPath.c_str());
string content((const char*)fileData.getBytes(), fileData.getSize());

// this would call your own Parser class based on my example above:
gameInfo = Parser::parseGameData(content);
1 Like

One less line of code:

string content = FileUtils::getInstance()->getStringFromFile(fullPath.c_str());

this loads a teminated string.

Yes, what is the correct way then?

rapidjson::Document d;
std::string file = "res/data.json";
std::string content = FileUtils::getInstance()->getStringFromFile(file.c_str());
log("file content: %s", content.c_str());
d.Parse<0>(content.c_str());

File is a 584kb json. It’s partially loaded(terminated). That’s why can not be parsed. Am I missing something?