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、通用解析方法:
- char *json = "{\"RootA\":\"Value in parent node\",\"ChildNode\":{\"ChildA\":\"String Value\",\"ChildB\":42}}";
- JSONNODE *n = json_parse(json);
- ParseJSON(n);
- json_delete(n);
- void ParseJSON(JSONNODE *n){
- if (n == NULL){
- printf("Invalid JSON Node\n");
- return;
- }
- JSONNODE_ITERATOR i = json_begin(n);
- while (i != json_end(n)){
- if (*i == NULL){
- printf("Invalid JSON Node\n");
- return;
- }
- // recursively call ourselves to dig deeper into the tree
- if (json_type(*i) == JSON_ARRAY || json_type(*i) == JSON_NODE){
- ParseJSON(*i);
- }
- // get the node name and value as a string
- json_char *node_name = json_name(*i);
- // find out where to store the values
- if (strcmp(node_name, "RootA") == 0){
- json_char *node_value = json_as_string(*i);
- strcpy(rootA, node_value);
- json_free(node_value);
- }
- else if (strcmp(node_name, "ChildA") == 0){
- json_char *node_value = json_as_string(*i);
- strcpy(childA, node_value);
- json_free(node_value);
- }
- else if (strcmp(node_name, "ChildB") == 0)
- childB = json_as_int(*i);
- // cleanup and increment the iterator
- json_free(node_name);
- ++i;
- }
- }
b,通用拼接json:
数组:
- JSONNODE *n = json_new(JSON_NODE);
- json_push_back(n, json_new_a("RootA", "Hello World"));
- JSONNODE *c = json_new(JSON_ARRAY);
- json_set_name(c, "ArrayOfNumbers");
- json_push_back(c, json_new_i(NULL, 16));
- json_push_back(c, json_new_i(NULL, 42));
- json_push_back(c, json_new_i(NULL, 128));
- json_push_back(n, c);
- json_char *jc = json_write_formatted(n);
- printf("%s\n", jc);
- json_free(jc);
- json_delete(n);
- {
- "RootA" : "Hello World",
- "ArrayOfNumbers" : [
- 16,
- 42,
- 128
- ]
- }
非数组:
- JSONNODE *n = json_new(JSON_NODE);
- json_push_back(n, json_new_a("RootA", "Value in parent node"));
- JSONNODE *c = json_new(JSON_NODE);
- json_set_name(c, "ChildNode");
- json_push_back(c, json_new_a("ChildA", "String Value"));
- json_push_back(c, json_new_i("ChildB", 42));
- json_push_back(n, c);
- json_char *jc = json_write_formatted(n);
- printf("%s\n", jc);
- json_free(jc);
- json_delete(n);
- {
- "RootA" : "Value in parent node",
- "ChildNode" : {
- "ChildA" : "String Value",
- "ChildB" : 42
- }
- }