Cocos2d-x Method of Making JSON string, cocos2d-xjson
Address: http://blog.csdn.net/qqmcy/article/details/39152695
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. h "USING_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. h "bool 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 ("Age", "111", allocator); array. pushBack (object, allocator); d1.AddMember ("propety", "PLAYER-TO", allocator); d1.AddMember ("player", array, allocator); rapidjson: StringBuffer buffer; rapidjson: Writer <rapidjson: StringBuffer> write (buffer); d1.Accept (write); return StringUtils: format ("% s", buffer. getString ());}
C ++ cocos2dx common method for parsing json strings
If the json string format is the same, but the parameters are different, you can write a common parsing function. For different json string formats, you have to write them one by one.
What should I do if the JSON string is parsed into jsonAray? The string is as follows:
JSONArray objarry = obj. getJSONArray ("response ");
The focus is to use the getJSONArray method and then use the for Loop
I will give you a piece of instance code. You can study it yourself
JSONArray listjson = insjson. getJSONObject ("data"). getJSONArray (
"Loglist ");
For (int I = 0; I <listjson. length (); I ++ ){
Map <String, String> insmap = new HashMap <String, String> ();
JSONObject jsonitem = listjson. getJSONObject (I );
Insmap. put ("id", jsonitem. getString ("id "));
Insmap. put ("message_type", msgpe. selectItem (jsonitem. getString ("message_type"). trim ()));
}