游戏开发中,需要使用大量的数据解析,json是一种轻量级的数据结构,以键、值的方式存储数据,呵呵,使用方便啊。
传说中的libJson和jsoncpp,是神马也没搞太清楚,但是看了jsonBox就很喜欢,操作简单。这里要感谢anhero的贡献啊。
还有就是jsonBox的环境配置问题。
jsonBox的github地址:https://github.com/anhero/JsonBox
下载完成后,将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 关键字。
好的,现在看下.h文件
#include <iostream>
#include "curl.h"
#include "JsonBox.h"
using namespace std;
class ParserJsonData {
public:
static string returnData;
public:
void requestDataFromServer(string Url);
static size_t writeData(void *ptr, size_t size, size_t nmemb, void *userData);
void parserReturnData(string jsonData);
};
然后是.cpp文件
#include "ParserJsonData.h"
string ParserJsonData::returnData = "";
void ParserJsonData::requestDataFromServer(string Url)
{
CURL *easyHandle;
//初始化curl句柄
easyHandle = curl_easy_init();
if (easyHandle) {
//设置超时时间10s
curl_easy_setopt(easyHandle, CURLOPT_TIMEOUT, 10L);
//设置网络访问地址
curl_easy_setopt(easyHandle, CURLOPT_URL, Url.c_str());
//设置在控制台打印网络请求详细信息
curl_easy_setopt(easyHandle, CURLOPT_VERBOSE, 1L);
//设置回调函数
curl_easy_setopt(easyHandle, CURLOPT_WRITEFUNCTION, ParserJsonData::writeData);
}
curl_easy_perform(easyHandle);
curl_easy_cleanup(easyHandle);
string temp = returnData;
this->parserReturnData(temp);
//将返回数据清空
returnData = "";
}
size_t ParserJsonData::writeData(void *ptr, size_t size, size_t nmemb, void *userData)
{
string temp = (char*)ptr;
returnData = returnData + temp;
cout<<returnData<<endl;
return size * nmemb;
}
//主要的jsonBox解析就在这里了
void ParserJsonData::parserReturnData(string jsonData)
{
JsonBox::Value json;
json.loadFromString(jsonData.c_str());
cout<<json<<endl;
int size = json["list"].getArray().size();
for (int i=0; i<size; i++) {
cout<<json["list"][size_t(i)]["name"]<<endl;
cout<<json["list"][size_t(i)]["id"]<<endl;
cout<<json["list"][size_t(i)]["price"]<<endl;
}
}
JsonBox有专门用来将自己value值转化成int、string、double等常用数据类型的方法:
这里把返回的数据和解析后的数据打印出来:
2013-08-01 10:55:01.048 JsonTest[1616:c07] cocos2d: surface size: 960x640
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>
* About to connect() to 192.168.1.5 port 80 (#0)
* Trying 192.168.1.5...
* connected
* Connected to 192.168.1.5 (192.168.1.5) port 80 (#0)
> GET /props/list.page HTTP/1.1
Host: 192.168.1.5
Accept: */*
< HTTP/1.1 200 OK
< Set-Cookie: JSESSIONID=C4E46DEA68A2E5DC4C695F0E10D285D5.jvm1; Path=/
< Content-Type: text/html;charset=UTF-8
< Transfer-Encoding: chunked
< Date: Thu, 01 Aug 2013 02:55:04 GMT
< Server: Apache-Coyote/1.1
<
{"list":[{"updateTime":0,"icon":"1.png","price":500,"name":"火球","id":1},{"updateTime":0,"icon":"2.png","price":100,"name":"闪电","id":2},{"updateTime":0,"icon":"4.png","price":200,"name":"子弹","id":4},{"updateTime":0,"icon":"3.png","price":800,"name":"激光","id":3},{"updateTime":0,"icon":"5.png","price":10,"name":"铁箭","id":5}]}
0
* Connection #0 to host 192.168.1.5 left intact
* Closing connection #0
{
"list" : [
{
"icon" : "1.png",
"id" : 1,
"name" : "火球",
"price" : 500,
"updateTime" : 0
},
{
"icon" : "2.png",
"id" : 2,
"name" : "闪电",
"price" : 100,
"updateTime" : 0
},
{
"icon" : "4.png",
"id" : 4,
"name" : "子弹",
"price" : 200,
"updateTime" : 0
},
{
"icon" : "3.png",
"id" : 3,
"name" : "激光",
"price" : 800,
"updateTime" : 0
},
{
"icon" : "5.png",
"id" : 5,
"name" : "铁箭",
"price" : 10,
"updateTime" : 0
}
]
}
"火球"
1
500
"闪电"
2
100
"子弹"
4
200
"激光"
3
800
"铁箭"
5
10
好的,到这里就解析完成,再次感谢他的作者,anhero。