cocos2d-x 2.1.3 使用json

1.解析同等级:
const
char* text= "{\"record\":{\"data\":20130101,\"rate\":23,\"buy\":1},\"record\":{\"data\":45,\"rate\":67,\"buy\":1},\"record\":{\"data\":45,\"rate\":76,\"buy\":10}}"; Json* root= Json_create(text); Json* _record=root->child; while(_record) { Json* _date=Json_getItem(_record, "data"); if(_date->type==Json_Number) { int date= _date->valueint; CCLOG("Date:%d",date); } Json* _rate=Json_getItem(_record, "rate"); if(_rate->type==Json_Number) { int rate=_rate->valueint; CCLOG("Rate:%d",rate); } Json* _buy=Json_getItem(_record, "buy"); if(_buy->type==Json_Number) { int buy=_buy->valueint; CCLOG("Buy:%d",buy); } _record=_record->next; } Json_dispose(root);

2.解析不同等级
char* text="{\"status\":10,\"body\":{\"force\":5,\"latest\":{\"ver\":\"2.0\",\"ver_num\":\"200\"}}}";
    
    Json* _root= Json_create(text);
    
    Json* _status = Json_getItem(_root, "status");
    if(_status->type==Json_Number)
    {
        int status_text= _status->valueint;
        CCLOG("status_text:%d",status_text);
    }
    
    Json* _bodyArray=Json_getItem(_root, "body");
    if(_bodyArray->type==Json_Object)
    {
        Json* _force=Json_getItem(_bodyArray, "force");
        if(_force->type==Json_Number)
        {
            int _force_text=_force->valueint;
            CCLOG("_force_text:%d",_force_text);
        }

        Json* _latestArray=Json_getItem(_bodyArray, "latest");
        if(_latestArray->type==Json_Object)
        {
            Json* _ver=Json_getItem(_latestArray, "ver");
            if(_ver->type==Json_String)
            {
                const char* _ver_text=_ver->valuestring;
                CCLOG("_ver_text:%s",_ver_text);
            }
            
            Json* _ver_num=Json_getItem(_latestArray, "ver_num");
            if(_ver_num->type==Json_String)
            {
                const char* _ver_num_text=_ver_num->valuestring;
                CCLOG("_ver_num_text:%s",_ver_num_text);
            }
        }

    }

    Json_dispose(_root);

 



备注:
在此发现一个json文件处理整形数字的一个问题,数值比较大的话,就会不准确,如 20130101 获取的值会被减去1 结果为:20130100 ,看了一下源代码,好像是因为在计算的时候会按照float类型计算,有些数据很有可能被舍弃了,所以为了保险起见 还是将数字都作为字符串来处理的好

 

posted on 2013-05-19 21:26  Games  阅读(2704)  评论(0编辑  收藏  举报