The previous blog tells how to use Rapidjson to read Chinese strings, but one obvious flaw is the inability to directly read data exported by the data editor. Let's fix this bug and reduce the code to half the size of the original.
The previous blog address is: "COCOS2DX" Rapidjson How to use and Chinese display solution
Not many say directly on the source bar:
First, the format that the data editor exports is:
[{"id": 1, "Content": "Press the back key again to exit the game"},{"id": 2, "Content": "Gobang"},{"id": 3, "content": "Black Square"},{"id": 4, "Content": "White Square"},{ "id": 5, "Content": "Current Player"},{"id": 6, "content": "Tip:"},{"id": 7, "content": "null"},{"ID": 8, "content": "null"},{"ID": 9 , "content": "null"},{"ID": Ten, "content": "null"},{"id": One, "content": "null"},{"id": "content": "null"},{"id": 13, " Content ":" "null"},{"id": "content": "null"},{"id": "" "," content ":" null "},{" id ":" content ":" null "},{" id ": 17," Content ":" null "}]
When exporting, select Export as Attribute
Resolved classes:
#pragma once/** get the String class Singleton * To get the Chinese string in the JSON file */#include "cocos2d.h" #include <string> #include <cocos-ext.h> #include "cocostudio\cocostudio.h" #include "ui\cocosgui.h" USING_NS_CC; Using_ns_cc_ext;using namespace std;using namespace cocostudio;/*** the macro definition of the data ID, used to find the Chinese data */static int s_iexit = 1; Press the back key again to exit the game class Readstring{protected:rapidjson::D ocument m_doc; Staged data static readstring* rs;public:static readstring* getinstance () std::string parse (int key);p rivate:static readstring* creat (std::string filename); bool init (std::string filename); ReadString () {};~readstring (void) {};};
Then the CPP:
#include "ReadString.h" readstring* readstring::rs = nullptr; Initialize readstring* readstring::creat (std::string fileName) {ReadString *rs = new ReadString (); RS | | ! Rs->init (fileName)) {delete RS; RS = Nullptr;return nullptr;} return RS;} BOOL Readstring::init (std::string fileName) {BOOL ret = false;do {std::string Jsonpath = fileutils::getinstance () Fullpathforfilename (fileName); std::string contentstr = Fileutils::getinstance ()->getstringfromfile (Jsonpath); m _doc. Parse<0> (Contentstr.c_str ()); Cc_break_if (M_doc. Hasparseerror ()); ret = true;} while (0); return ret;} readstring* readstring::getinstance () {if (rs = = nullptr) rs= readstring::creat ("String.json"); return RS;} String ReadString::p arse (int key) {if (!m_doc. IsNull () && M_doc. IsArray ())//judgment is not an array {for (unsigned int i=0;i<m_doc. Size (); ++i) {do {cc_break_if (M_doc[i]). IsNull ()); Cc_break_if (m_doc[i]["id"). IsNull ()); int i_id = m_doc[i]["id"]. GetInt (); CC_BREAK_IF (i_id! = key); This line is looking for the same key, found to execute the following statement, can not find the start of the next loop cc_break_IF (m_doc[i]["content"]. IsNull ()); return m_doc[i]["content". GetString ();} while (0);}} Return ""; No object found. Returns an empty string}
The read function in the previous blog was changed to parse, but the function was the same. But isn't it nice to see a lot of ha. (Well, it's still so ugly)
The same approach is used:
Readstring::getinstance ()->parse (s_iexit)
"Cocos2dx" Rapidjson method of use and Chinese display resolution "cont."