Cocos2d-x parses json data into arrays or dictionaries (libjson Library)

Source: Internet
Author: User

Previously in the cocos2d-x project using json parsing, integrated libjson library found that most of the parsing methods provided on the Internet get value in the parsing process, and does not save the parsing results effectively, as a result, I tried to save the parsing result to an array or dictionary based on the data format.

Do not dare to exclusive, the Code is available:

JsonUtil: parseArrJSON (JSONNODE * n, CCObject ** (n = isAry = JSONNODE_ITERATOR it = * key = json_name (* (strcmp (key ,) = * obj = (it! = (* It = (json_type (* it) = * char_str = CCString: ccs (*) * obj)-> key = json_name (*! *) * Obj)-> (json_type (* it) = JSON_ARRAY | json_type (* it) = * tempobj = * it, & *) * obj) -> key = json_name (*! *) * Obj)-> (json_type (* it) = * value = json_as_string (** char_str = *) * obj)-> key = json_name (*! *) * Obj)-> (json_type (* it) = json_number value = json_as_float (** char_str = CCString: createWithFormat (*) * obj) -> key = json_name (*! *) * Obj)-> + * JsonUtil: jsonValue (** an = * obj = &View Code

Create a class JsonUtil, which contains two static methods. Call JsonUtil: jsonValue (const char * _ char) when using JsonUtil. The parsed results are saved in the dictionary or array. For example:

  *betem=CCArray  *ary= (CCArray *)   JsonUtil::jsonValue(betem);
ccout(ary);

Ccout is a custom method for printing arrays and dictionaries. The cocos2d-x's CCLog is really far behind the NSLog, even if it prints out the time.

The attached ccout code can be used to print the json data stored in CCArray or CCDictionary, barely like the NSLog output:

  

Ccout (CCObject * thob, donotset =, dot = AppDelegate: ccout (CCObject * thob, donotset, (dynamic_cast <CCDictionary *> <* temp = (CCDictionary *** key = ele-> * ob = ele-> <(I =; I <donotset +; I ++) cout <key <-> ccout (ob, donotset + <(I =; I <donotset; I ++) cout <(dynamic_cast <CCArray *> <* temp = (CCArray ** (dynamic_cast <CCDictionary *> (ob! = Temp-> ccout (ob, donotset +,-> ccout (ob, donotset + (dynamic_cast <CCArray *>-> ccout (ob, donotset + (dynamic_cast <CCString *> <(CCString *) ob)-> (ob! = Temp-> <View Code

Output result:

======

 

 

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.