cocos2d-x 使用libjson解析json--------------cocos2d-x 3.0正式版本(7.6)

cocos2d-x自带了json解析方法,也可以很方便进行json操作,使用方法

1,引入json.h头文件

2,代码示例:

//"svrId":6,

//"name":"Andromeda&Pandora",

//"ip":"69.162.106.170",

//"port":8892,


//"capacity":20,

//"status":1,

//"language":"2",

//"payType":1,


//"playerNums":10230,

//"onlineNums":361,

//"cfg":"ac:y,s:1,h:y"

voidHelloWorld::parseJsonObjects(constchar* pJsonStr) {

   Json* jsonRoot = Json_create(pJsonStr);

   Json* jsonChild = jsonRoot->child;

   servers = newvector<constchar*>();

   while (jsonChild) {

       if (jsonChild->type==Json_Object) {

           Json* svrId = Json_getItem(jsonChild,"svrId");

           CCLog("svrId:%d", svrId->valueint);

           Json* name = Json_getItem(jsonChild,"name");

           CCLog("name:%s", name->valuestring);

            

            // push server's name into vector

           servers->push_back(name->valuestring);

            

           Json* ip = Json_getItem(jsonChild,"ip");

           CCLog("ip:%s", ip->valuestring);

           Json* port = Json_getItem(jsonChild,"port");

           CCLog("port:%d", port->valueint);

           Json* capacity = Json_getItem(jsonChild,"capacity");

           CCLog("capacity:%d", capacity->valueint);

            

           Json* status = Json_getItem(jsonChild,"status");

           CCLog("status:%d", status->valueint);

           Json* language = Json_getItem(jsonChild,"language");

           CCLog("language:%s", language->valuestring);

           Json* payType = Json_getItem(jsonChild,"payType");

           CCLog("payType:%d", payType->valueint);

            

           Json* playerNums = Json_getItem(jsonChild, "playerNums");

           CCLog("playerNums:%d", playerNums->valueint);

           Json* onlineNums = Json_getItem(jsonChild, "onlineNums");

           CCLog("onlineNums:%d", onlineNums->valueint);

           Json* cfg = Json_getItem(jsonChild,"cfg");

           CCLog("cfg:%s", cfg->valuestring);

            

           printf("\n\r");

        }

        jsonChild = jsonChild->next;

    }

    CCLog("SERVERSLENGTH:%ld",servers->size());

   Json_dispose(jsonRoot);

}


自带库能够快速解析json,但是如果想拼json,json.h无法实现,下面演示使用Libjson解析json:

1,下载Libjson库,最新版本7.6.1

2,引入文件到工程项目里引入方法,直接copy libjson中 _internal文件夹,JSONOptions.h, libjson.h到自建文件夹,

然后把文件夹复制到工作目录里头lib中,添加到项目中去

3,引用完毕后编译会报一个错误,这中因为Demo中引用路径有些问题,直接删掉_internal文件夹中的两Demo


基本使用方法:

a、通用解析方法:

[cpp]  view plain copy
  1. char *json = "{\"RootA\":\"Value in parent node\",\"ChildNode\":{\"ChildA\":\"String Value\",\"ChildB\":42}}";  
  2. JSONNODE *n = json_parse(json);  
  3. ParseJSON(n);  
  4. json_delete(n);  

[cpp]  view plain copy
  1. void ParseJSON(JSONNODE *n){  
  2.     if (n == NULL){  
  3.         printf("Invalid JSON Node\n");  
  4.         return;  
  5.     }  
  6.    
  7.     JSONNODE_ITERATOR i = json_begin(n);  
  8.     while (i != json_end(n)){  
  9.         if (*i == NULL){  
  10.             printf("Invalid JSON Node\n");  
  11.             return;  
  12.         }  
  13.    
  14.         // recursively call ourselves to dig deeper into the tree  
  15.         if (json_type(*i) == JSON_ARRAY || json_type(*i) == JSON_NODE){  
  16.             ParseJSON(*i);  
  17.         }  
  18.    
  19.         // get the node name and value as a string  
  20.         json_char *node_name = json_name(*i);  
  21.    
  22.         // find out where to store the values  
  23.         if (strcmp(node_name, "RootA") == 0){  
  24.             json_char *node_value = json_as_string(*i);  
  25.             strcpy(rootA, node_value);  
  26.             json_free(node_value);  
  27.         }  
  28.         else if (strcmp(node_name, "ChildA") == 0){  
  29.             json_char *node_value = json_as_string(*i);  
  30.             strcpy(childA, node_value);  
  31.             json_free(node_value);  
  32.         }  
  33.         else if (strcmp(node_name, "ChildB") == 0)  
  34.             childB = json_as_int(*i);  
  35.    
  36.         // cleanup and increment the iterator  
  37.         json_free(node_name);  
  38.         ++i;  
  39.     }  
  40. }  

b,通用拼接json:

数组:

[cpp]  view plain copy
  1. JSONNODE *n = json_new(JSON_NODE);  
  2. json_push_back(n, json_new_a("RootA""Hello World"));  
  3. JSONNODE *c = json_new(JSON_ARRAY);  
  4. json_set_name(c, "ArrayOfNumbers");  
  5. json_push_back(c, json_new_i(NULL, 16));  
  6. json_push_back(c, json_new_i(NULL, 42));  
  7. json_push_back(c, json_new_i(NULL, 128));  
  8. json_push_back(n, c);  
  9. json_char *jc = json_write_formatted(n);  
  10. printf("%s\n", jc);  
  11. json_free(jc);  
  12. json_delete(n);  

[cpp]  view plain copy
  1. {  
  2.     "RootA" : "Hello World",  
  3.     "ArrayOfNumbers" : [  
  4.         16,  
  5.         42,  
  6.         128  
  7.     ]  
  8. }  

非数组:

[cpp]  view plain copy
  1. JSONNODE *n = json_new(JSON_NODE);  
  2. json_push_back(n, json_new_a("RootA""Value in parent node"));  
  3. JSONNODE *c = json_new(JSON_NODE);  
  4. json_set_name(c, "ChildNode");  
  5. json_push_back(c, json_new_a("ChildA""String Value"));  
  6. json_push_back(c, json_new_i("ChildB", 42));  
  7. json_push_back(n, c);  
  8. json_char *jc = json_write_formatted(n);  
  9. printf("%s\n", jc);  
  10. json_free(jc);  
  11. json_delete(n);  

[cpp]  view plain copy
  1. {  
  2.     "RootA" : "Value in parent node",  
  3.     "ChildNode" : {  
  4.         "ChildA" : "String Value",  
  5.         "ChildB" : 42  
  6.     }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值