Cocos2d-x Method of Making JSON string, cocos2d-xjson

Source: Internet
Author: User

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 ()));
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.