写一个接口,由于同事是android的,用JSON比较方便,使用了,便于他调用。
接口代码较烦。
需要头文件cJSON.h,cJSON.c,
一个实验代码:
/*
Copyright (c) 2009 Dave Gamble
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include <stdio.h>
#include <stdlib.h>
#include "cJSON.h"
/* Parse text to JSON, then render back to text, and print! */
//1、使用返回值进行,返回jSON*
/* cJSON *doit(char *text)
{
char *out;//cJSON *json;
int flag = 0;
cJSON *c = cJSON_Parse(text);
//printf("text = %s\n", text);
return c;
}
*/
//2、使用jSON**给形参
void doit2(char *text,cJSON** root)
{
char *out;
*root = cJSON_Parse(text);
printf("doit2 text = %s\n", text);
}
//3、
/*void doit3(char *text,cJSON *root)
{
char *out;
// cJSON *temp ;printf("BBBB\n");
//root = cJSON_CreateObject();
// temp = cJSON_Parse(text);printf("CCCC\n");
// root = temp;printf("DDDD\n");
root = cJSON_Parse(text);printf("CCCC\n");
printf("333 root->type 1:= %d\n", root->type);
printf("doit3 text = %s\n", text);
out=cJSON_Print(root);
cJSON_Delete(root);
printf("doit3:%s\n",out);
}*/
//3、使用malloc分配空间
/*void doit3(char *text,cJSON *root)
{
char *out;
root = cJSON_Parse(text);
printf("doit2 text = %s\n", text);
out=cJSON_Print(root);
cJSON_Delete(root);
printf("doit3:%s\n",out);
}*/
int main (int argc, const char * argv[])
{
/* a bunch of json: */
char sensorValue[] = "{\"WIFIessid\":[{\"ESSID\":\"ylst-s\"},{\"ESSID\":\"mordenmansion\"},{\"ESSID\":\"ChinaNet-wvQS\"},{\"ESSID\":\"CMCC\"},{\"ESSID\":\"CMCC-AUTO\"},{\"ESSID\":\"beacon05\"},{\"ESSID\":\"cnswx\"}]}";
//cJSON *root = (cJSON*)malloc(sizeof(cJSON));
//1、使用返回值进行,返回cJSON*
/* cJSON *root;
char *out;
root = doit(sensorValue);
printf("222 root->type = %d\n", root->type);
out=cJSON_Print(root);
cJSON_Delete(root);
printf("doit:%s\n",out);
*/
//2、使用cJSON**给形参
cJSON *root;
char *out;
doit2(sensorValue,&root);
printf("222 root->type = %d\n", root->type);
out=cJSON_Print(root);
cJSON_Delete(root);
printf("doit2:%s\n",out);
3、
/* cJSON root ;printf("0000\n");
char *out;printf("AAAA\n");
doit3(sensorValue,&root);printf("EEEE\n");
printf("333 root->type = %d\n", root.type);printf("FFFF\n");
out=cJSON_Print(&root);printf("FFFF\n");
cJSON_Delete(&root);printf("FFFF\n");
printf("doit3:%s\n",out);printf("FFFF\n");
*/
//3、使用malloc分配空间
/* cJSON *root = (cJSON)malloc(sizeof(cJSON));
char *out;
doit3(sensorValue,&root);
printf("222 root->type = %d\n", root.type);
out=cJSON_Print(&root);
cJSON_Delete(&root);
printf("doit2:%s\n",out);
*/
//free(out);
//char * out= NULL;printf("AAABBB\n");
//out = cJSON_Print(root);printf("AAABBB\n");
//printf("AAAAAAAAAAA\n%s\n",out);
return 0;
}