How to Create a JSON string using a Cocos2d-x
This section describes how to compile a JSON string. Directly accessing the code is relatively simple.
MakeJson. h
//// MakeJson. h // cpp4 // Created by du jia on 14-9-9. /// # ifndef _ cpp4 _ MakeJson __# define _ cpp4 _ MakeJson __# include cocos2d. h # include json/document. h # include json/writer. h # include json/stringbuffer. hUSING_NS_CC; class MakeJson: public Ref {public: CREATE_FUNC (MakeJson); virtual bool init (); std: string getTestJson ();}; # endif/* defined (_ cpp4 _ MakeJson __)*/
MakeJson. cpp
//// MakeJson. cpp // cpp4 // Created by du jia on 14-9-9. //// # include MakeJson. hbool MakeJson: init () {bool bRef = false; do {bRef = true;} while (0); return bRef;} std: string MakeJson: getTestJson () {auto path = FileUtils: getInstance ()-> getWritablePath (); log (% s, path. c_str (); // Add a json file path under this path. append (test. json); rapidjson: Document d1; d1.SetObject (); rapidjson: Document: AllocatorType & allocator = d1.GetAllocator (); rapidjson: Value array (rapidjson: kArrayType ); rapidjson: Value object (rapidjson: kObjectType); object. addMember (id, 1, allocator); object. addMember (name, 234, allocator); object. addMember (ages, 111, allocator); array. pushBack (object, allocator); d1.AddMember (propety, PLAYER-TO, allocator); d1.AddMember (player, array, allocator); rapidjson: StringBuffer; rapidjson: Writer
Write (buffer); d1.Accept (write); return StringUtils: format (% s, buffer. GetString ());}