Using .json files
We can use the JSON format like the XML format for saving/loading game-related data. JSON is a simpler format than XML. It takes less space to represent the same data than the XML file format. Further, today, it is used as the value of Web API. Cocos2d-x has a JSON parse library called RapidJSON. In this recipe, we will explain how to use RapidJSON.
Getting ready
RapidJSON is usually included in Cocos2d-x. However, you need to include the header files as follows:
#include "json/rapidjson.h" #include "json/document.h"
How to do it...
Firstly, we will parse a JSON string as follows:
std::string str = "{\"hello\" : \"word\"}";
You can parse JSON by using rapidjson::Document
as follows:
rapidjson::Document d; d.Parse<0>(str.c_str()); if (d.HasParseError()) { CCLOG("GetParseError %s\n",d.GetParseError()); } else if (d.IsObject() && d.HasMember("hello")) { CCLOG("%s\n", d["hello"].GetString()); }
How it works...
You can parse JSON by using the Document::Parse
...