游戏开发中,需要使用大量的数据解析,json是一种轻量级的数据结构,以键、值的方式存储数据,呵呵,使用方便啊。
传说中的libJson和jsoncpp,是神马也没搞太清楚,但是看了jsonBox就很喜欢,操作简单。这里要感谢anhero的贡献啊。
还有就是jsonBox的环境配置问题。
jsonBox的github地址:https://github.com/anhero/JsonBox
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLiIXZ05WZD9CX5RXa2Fmcn9CXwczLcVmds92czlGZvwVP9EUTDZ0aRJkSwk0LcxGbpZ2LcBDM08CXlpXazRnbvZ2LcRlMMVDT2EWNvwFdu9mZvwVerpWT0kleOFTQq5Ed5ckW6xmMkZXUYpVd1kmYr50MZV3YyI2cKJDT29GRjBjUIF2LcRHelR3LcJzLctmch1mclRXY39zN4YTN1ATN3ETNyYDMzEDMy8CX0Vmbu4GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.jpg)
下载完成后,将src和include两个文件夹拿出来,那里面是jsonBox的类库。其他的暂时不管。
这里可以看到我们需要的类库jsonBox的.h和.cpp文件在这2个文件夹下面。
按照网上一篇文章,将我的环境配置了半天没有作用,还是文件路径的问题。没办法,我一着急,直接把这些类全部放在一个文件夹里面。第一个ds——store是Mac的隐藏文件。当然这样需要修改的引入的问题,就是把#include<json/array.h>改为#include<array.h>。
这里需要在target里面引入路径问题,build setting 里面输入header search。
好的,到这里,环境就搭建完成了。现在来看看怎么使用吧,代码才是王道,忘记谁说的了。
这里声明libCurl里面的两个方法,libCurl在C++里面必须使用static 关键字。
#include "cocos2d.h"
#include <iostream.h>
#include <curl.h>
#include "JsonBox.h"
class HelloWorld : public cocos2d::CCLayer
{
public:
static void begin();
static size_t writeData(void *ptr,size_t size, size_t nmemb, void *userData);
};
void HelloWorld::begin()
{
char *serverIP = "http://192.168.1.112/jforum?module=shoot&action=getlevelinfo";
char *serverIP2 = "http://192.168.1.112/jforum?module=shoot&action=start";
char *serverIP3 = "http://weather.china.xappengine.com/api?city=beijing";
CURL *curl5;
char *data = "name=Kevin&passWord=5678";
curl5 = curl_easy_init();
curl_easy_setopt(curl5, CURLOPT_URL,serverIP3);
//curl_easy_setopt(curl5, CURLOPT_POSTFIELDS,data);
curl_easy_setopt(curl5, CURLOPT_WRITEFUNCTION,HelloWorld::writeData);
curl_easy_perform(curl5);
curl_easy_cleanup(curl5);
putchar(10);
}
size_t HelloWorld::writeData(void *ptr, size_t size, size_t nmemb, void *userData)
{
static int flag = 1;
FILE *fp = fopen("/Users/liyongjian/Desktop/test.text", "a");
char *data = NULL;
data = (char *)ptr;
int writeen = fwrite(ptr, size, nmemb, fp);
CCLog("close");
printf("write_data 第%d次\n",flag++);
printf("写入字节数量是 %d\n",writeen);
printf("data**********:\n%s",data);
putchar(10);
printf("*********************************************\n");
fclose(fp);
//解析jsonBox
JsonBox::Value V2;
V2.loadFromString(data);
CCLOG("*************************************************");
//char *temp1= (void *)V2["pub"];
std::cout<<"pub: "<< V2["pub"]<<endl;
std::cout<<"name: "<< V2["name"]<<endl;
std::cout<<"speed: "<< V2["wind"]["speed"]<<endl;
string pub1 = V2["pub"].getString();
char *pub2 = (char*)pub1.c_str();
CCLOG("temp1 :%s",pub1.c_str());
CCLOG("temp2 :%s",pub2);
string name1 = V2["name"].getString();
char *name2 = (char*)name1.c_str();
CCLOG("name1 :%s",name1.c_str());
CCLOG("name2 :%s",name2);
int wind = V2["wind"]["speed"].getInt();
CCLOG("wind :%d",wind);
return written;
}
JsonBox有专门用来将自己value值转化成int、string、double等常用数据类型的方法:
这里解析出来这个天气API的时间、地点和风力等级:
2013-06-25 17:20:28.350 jsonCurlTest[1825:12203] cocos2d: surface size: 480x320
Cocos2d:
<dict>
cocos2d.x.version: 2.1rc0-x-2.1.4
cocos2d.x.compiled_with_profiler: false
cocos2d.x.compiled_with_gl_state_cache: true
gl.vendor: Apple Computer, Inc.
gl.renderer: Apple Software Renderer
gl.version: OpenGL ES 2.0 APPLE
gl.max_texture_size: 4096
gl.max_texture_units: 8
gl.max_samples_allowed: 4
gl.supports_PVRTC: true
gl.supports_NPOT: true
gl.supports_BGRA8888: false
gl.supports_discard_framebuffer: true
gl.supports_vertex_array_object: true
</dict>
Cocos2d: close
write_data 第1次
写入字节数量是 1181
data**********:
{"pub" : "2013-06-25 13:59", "name" : "北京", "wind" : {"chill" : 75, "direction" : 0, "speed" : 2}, "astronomy" : {"sunrise" : "4:44", "sunset": "19:45"}, "atmosphere" : {"humidity" : 78, "visibility" : 1.74, "pressure" : 29.53, "rising": 0}, "forecasts" : [{"date" : "2013-06-25", "day" : 2, "code" : 29, "text" : "局部多云", "low" : 21, "high" : 29, "image_large" : "http://weather.china.xappengine.com/static/w/img/d29.png", "image_small" : "http://weather.china.xappengine.com/static/w/img/s29.png"}, {"date" : "2013-06-26", "day" : 3, "code" : 30, "text" : "局部多云", "low" : 22, "high" : 34, "image_large" : "http://weather.china.xappengine.com/static/w/img/d30.png", "image_small" : "http://weather.china.xappengine.com/static/w/img/s30.png"}, {"date" : "2013-06-27", "day" : 4, "code" : 34, "text" : "晴朗", "low" : 22, "high" : 34, "image_large" : "http://weather.china.xappengine.com/static/w/img/d34.png", "image_small" : "http://weather.china.xappengine.com/static/w/img/s34.png"}, {"date" : "2013-06-28", "day" : 5, "code" : 38, "text" : "零星雷雨", "low" : 22, "high" : 32, "image_large" : "http://weather.china.xappengine.com/static/w/img/d38.png",
*********************************************
Cocos2d: *************************************************
pub: "2013-06-25 13:59"
name: "北京"
speed: 2
Cocos2d: temp1 :2013-06-25 13:59
Cocos2d: temp2 :2013-06-25 13:59
Cocos2d: name1 :北京
Cocos2d: name2 :北京
Cocos2d: wind :2