在程序缓存中存入Json,再读取Json
- #include "spine/Json.h"
- #include "cocos-ext.h"
- #include "network/HttpClient.h"
- #include "network/HttpRequest.h"
- USING_NS_CC_EXT;
- USING_NS_CC;
- using namespace std;
- const char* txtJsData = "{ \"id\":\"12\" , \"user\":\"mar\" , \"fruits\":{\"apple\":199} }";
- Json* mJs = Json_create(txtJsData);
- Json* node = mJs->child;
- while (node)
- {
- const char* getName = node->valuestring;
- log("id: %s",getName);
- node = node->next;
- }
- Json* item = Json_getItem(mJs,"fruits");
- Json* itemNode = Json_getItem(item,"apple");
- int applePrice = itemNode->valueint;
- log("applePrice: %d",applePrice);
在网络上读取Json文件
- //客户端单例
- HttpClient* client = HttpClient::getInstance();
- //请求设定
- HttpRequest* httpReq = new HttpRequest();
- //以获取的方式进行请求
- httpReq->setRequestType(HttpRequest::Type::GET);
- //请求访问的地址
- httpReq->setUrl("http://s.doyo.cn/img/4f/03/f5a69e9e785572004231.jpg");
- //设置请求标记
- httpReq->setTag("ip");
- //设置请求有结果的时候的回调函数
- httpReq->setResponseCallback(this,SEL_HttpResponse(&HelloWorld::responseFinished));
- //客户端发出请求
- client->send(httpReq);
- //设定请求的时限 时限到了就抛出错误 结束请求
- client->setTimeoutForConnect(0.000001);
- //释放过时的请求数据
- httpReq->release();
- void HelloWorld::responseFinished(Node* n,Object* obj)
- {
- log("get data");
- //obj:1.请求到的数据 2.当时请的标记 3.有请求本身 .....所有与请求有关信息
- HttpResponse* response = (HttpResponse*)(obj);
- if (false == response->isSucceed()) //判断请求是否失败 如失败 return
- {
- log("err close.");
- return;
- }
- HttpRequest* request = response->getHttpRequest();
- //获取当前接到的信息所使用的请求的标记
- const char* tag = request->getTag();
- std::vector<char>* info = response->getResponseData();
- std::string infoStr = "";
- //strcmp用于比较两个char* 1.如果相同 返回0 2.如果前一个比后一个小 返回-1 3.如果前一个比后一个大 返回1
- if (0 == strcmp(tag,"ip"))
- {
- infoStr = string(info->begin(),info->end());
- }
- log("info: %s",infoStr.c_str());
- /* 如果内容为文字 这里为图片
- Json* convert = Json_create(infoStr.c_str());
- Json* item = Json_getItem(convert,"origin");
- const char* itemInfo = item->valuestring;
- <span style="white-space:pre"> </span> */
- //获取不同平台的可写目录 + "设定的文件名"
- string wirtePath = FileUtils::getInstance()->getWritablePath() + "json.png";
- //通过File类的一系列函数进行对内存中的数据写入到本地存储设备
- FILE* openFile = fopen(wirtePath.c_str(),"wb+"); //打开文件 有即使用 无即创建
- //1.写入的内容 2.写入内容的单字节数 3.写入的内容的大小 4.写到哪个文件去
- fwrite(infoStr.c_str(),1,infoStr.size(),openFile);//对打开文件进行写入数据
- //写入完成之后关闭进程(文件)
- fclose(openFile);
如果Json内容为图片, 显示图片在舞台上
- //写入的文件大小
- unsigned long bffSize = 0;
- //获取指定地址的内容
- unsigned char* bff = FileUtils::getInstance()->getFileData(wirtePath.c_str(),"r",&bffSize);
- //Image是个图片类 所有Texture2D都用到
- Image* img = new Image();
- //本地的图片资源可以解析成字符数据
- img->initWithImageData(bff,bffSize);
- Texture2D* ttu2D = new Texture2D();
- ttu2D->initWithImage(img);
- Sprite* mm = Sprite::createWithTexture(ttu2D);
- addChild(mm);
- mm->setPosition(Point(100,100));
- //释放过时的内存
- img->release();
- ttu2D->release();
- //文件后缀
- span style="white-space:pre"> </span> string str = FileUtils::getInstance()->getWritablePath() + "json.png";
- std::string::size_type index = str.find_last_of(".");
- str = str.substr(index + 1);
- log("str: %s",str.c_str());