cJSON使用解析

代码下载 https://download.csdn.net/download/qq_25205045/10885751

  1. cJSON_Parse(pdata); //传递数据接口 提供一个JSON块,这将返回一个可以查询的CJSON对象。完成后调用return cJSON_ParseWithOpts(value,0,0)
    1.1cJSON_ParseWithOpts(const char *value,const char **return_parse_end,int require_null_terminated)解析一个对象-创建一个新的根,并填充
    1.1.1内部构造函数。cJSON_New_Item();动态分配空间 如果空间分配为0 返回0 end=parse_value(c,skip(value),ep); 解析器核心(解析字符串,数组,对象,null,false,true)-遇到文本结尾时,进行适当的处理。并更新c 如果end为0 释放内存 返回0
解析核心
/* Parser core - when encountering text, process appropriately. */
static const char *ICACHE_FLASH_ATTR
parse_value(cJSON *item,const char *value,const char **ep)
{
	if (!value)						return 0;	/* Fail on null. */
	if (!strncmp(value,"null",4))	{ item->type=cJSON_NULL;  return value+4; }
	if (!strncmp(value,"false",5))	{ item->type=cJSON_False; return value+5; }
	if (!strncmp(value,"true",4))	{ item->type=cJSON_True; item->valueint=1;	return value+4; }
	if (*value=='\"')				{ return parse_string(item,value,ep); }
	if (*value=='-' || (*value>='0' && *value<='9'))	{ return parse_number(item,value); }
	if (*value=='[')				{ return parse_array(item,value,ep); }
	if (*value=='{')				{ return parse_object(item,value,ep); }

	*ep=value;return 0;	/* failure. */
}
	1.1.2如果我们需要没有附加垃圾的以空结尾的JSON,请跳过,然后检查是否有空终止符。
	if (require_null_terminated) {end=skip(end);if (*end) {cJSON_Delete(c);*ep=end;return 0;}}
	if (return_parse_end) *return_parse_end=end;
  1. cJSON * Result = cJSON_CreateObject();//创建对象 还有其他的几种 见代码
/* Create basic types: */
cJSON * ICACHE_FLASH_ATTR cJSON_CreateNull(void)					{cJSON *item=cJSON_New_Item();if(item)item->type=cJSON_NULL;return item;}
cJSON * ICACHE_FLASH_ATTR cJSON_CreateTrue(void)					{cJSON *item=cJSON_New_Item();if(item)item->type=cJSON_True;return item;}
cJSON * ICACHE_FLASH_ATTR cJSON_CreateFalse(void)					{cJSON *item=cJSON_New_Item();if(item)item->type=cJSON_False;return item;}
cJSON * ICACHE_FLASH_ATTR cJSON_CreateBool(int b)					{cJSON *item=cJSON_New_Item();if(item)item->type=b?cJSON_True:cJSON_False;return item;}
cJSON * ICACHE_FLASH_ATTR cJSON_CreateNumber(double num)			{cJSON *item=cJSON_New_Item();if(item){item->type=cJSON_Number;item->valuedouble=num;item->valueint=(int)num;}return item;}
cJSON * ICACHE_FLASH_ATTR cJSON_CreateString(const char *string)	{cJSON *item=cJSON_New_Item();if(item){item->type=cJSON_String;item->valuestring=cJSON_strdup(string);if(!item->valuestring){cJSON_Delete(item);return 0;}}return item;}
cJSON * ICACHE_FLASH_ATTR cJSON_CreateArray(void)					{cJSON *item=cJSON_New_Item();if(item)item->type=cJSON_Array;return item;}
cJSON * ICACHE_FLASH_ATTR cJSON_CreateObject(void)					{cJSON *item=cJSON_New_Item();if(item)item->type=cJSON_Object;return item;}

  1. cJSON_AddNumberToObject(Result, “status”, 3);//继承对象
    cJSON_AddStringToObject(Result, “msg”, “json error!”); 其他继承见代码
/* Macros for creating things quickly. */
#define cJSON_AddNullToObject(object,name)		cJSON_AddItemToObject(object, name, cJSON_CreateNull())
#define cJSON_AddTrueToObject(object,name)		cJSON_AddItemToObject(object, name, cJSON_CreateTrue())
#define cJSON_AddFalseToObject(object,name)		cJSON_AddItemToObject(object, name, cJSON_CreateFalse())
#define cJSON_AddBoolToObject(object,name,b)	cJSON_AddItemToObject(object, name, cJSON_CreateBool(b))
#define cJSON_AddNumberToObject(object,name,n)	cJSON_AddItemToObject(object, name, cJSON_CreateNumber(n))
#define cJSON_AddStringToObject(object,name,s)	cJSON_AddItemToObject(object, name, cJSON_CreateString(s))

  1. char *succeedData = cJSON_Print(Result);//打印解析成功数据
/* Render a cJSON item/entity/structure to text. */
char *ICACHE_FLASH_ATTR
cJSON_Print(cJSON *item)				{return print_value(item,0,1,0);}
/* Render a value to text. */
static char *ICACHE_FLASH_ATTR
print_value(cJSON *item,int depth,int fmt,printbuffer *p)
{
	char *out=0;
	if (!item) return 0;
	if (p)
	{
		switch ((item->type)&255)
		{
			case cJSON_NULL:	{out=ensure(p,5);	if (out) os_strcpy(out,"null");	break;}
			case cJSON_False:	{out=ensure(p,6);	if (out) os_strcpy(out,"false");	break;}
			case cJSON_True:	{out=ensure(p,5);	if (out) os_strcpy(out,"true");	break;}
			case cJSON_Number:	out=print_number(item,p);break;
			case cJSON_String:	out=print_string(item,p);break;
			case cJSON_Array:	out=print_array(item,depth,fmt,p);break;
			case cJSON_Object:	out=print_object(item,depth,fmt,p);break;
		}
	}
	else
	{
		switch ((item->type)&255)
		{
			case cJSON_NULL:	out=cJSON_strdup("null");	break;
			case cJSON_False:	out=cJSON_strdup("false");break;
			case cJSON_True:	out=cJSON_strdup("true"); break;
			case cJSON_Number:	out=print_number(item,0);break;
			case cJSON_String:	out=print_string(item,0);break;
			case cJSON_Array:	out=print_array(item,depth,fmt,0);break;
			case cJSON_Object:	out=print_object(item,depth,fmt,0);break;
		}
	}
	return out;
}

char data[1024];
os_sprintf(data, “%s”, succeedData);

猜你喜欢

转载自blog.csdn.net/qq_25205045/article/details/85339759