自己用C语言写的JSON格式解析,搭配裸机更合适~

typedef struct{
	uint8_t key[20];
	uint8_t value[200];
}jsonDef;

jsonDef item;

uint8_t jsonData[]="{\"ABC\":22,\"FGH\":{\"III\":11},\"CDE\":\"44\"}";

void Json_Decode(uint8_t* key)
{
	uint8_t Findnext = 1;
	uint16_t len = sizeof(jsonData);
	uint16_t q;
	uint8_t shuanyinhaoPoint;
	uint8_t keyFound;
	uint8_t keyPoint;
	uint8_t pointlabel;
	uint8_t Includeinto;
	for(q=0;q<len;q++)
	{	
		if(shuanyinhaoPoint)
		{
			if(jsonData[q] == '"')
			{
				shuanyinhaoPoint--;
			}
			else
			{
				item.key[keyPoint] = jsonData[q];
				keyPoint++;
			}				
		}
		if((jsonData[q] == '"')&&(!keyPoint)&&(Findnext))
		{
			shuanyinhaoPoint++;
		}
		if((jsonData[q] == '"')&&(keyPoint))
		{
			keyPoint = 0;
			if(strstr((char*)(item.key),(char*)key))
			{
				keyFound = 1;
				Findnext = 0;
				memset(item.key,0,sizeof(item.key));
			}
			else
			{
				memset(item.key,0,sizeof(item.key));
				Findnext = 1;
			}
		}
		if(keyFound)
		{
			keyFound++;
		}
		if(keyFound == 4)
		{
			pointlabel = jsonData[q];

			if(pointlabel == '"')
				pointlabel = '"';
			else if(pointlabel == '{')
				pointlabel = '}';
			else 
				pointlabel = 1;
			
		}
		if(keyFound>3)
		{
			if(pointlabel == '}')
			{
				if(jsonData[q] == '{')
					Includeinto ++;
				if(jsonData[q] == '}')
					Includeinto --;
				item.value[keyFound-4] = jsonData[q];
				if((jsonData[q] == pointlabel)&&(Includeinto == 0))
				{
					keyFound = 0;
					pointlabel = 0;
				}
			}
			else if(pointlabel == '"')
			{
				if(jsonData[q] == '"')
					Includeinto ++;				
				if((jsonData[q] == pointlabel)&&(Includeinto == 2))
				{
					keyFound = 0;
					Includeinto  = 0;
					pointlabel = 0;
				}					
				if((Includeinto == 1)&&(jsonData[q] != '"'))
					item.value[keyFound-5] = jsonData[q];
			}
			else if(pointlabel == 1)
			{
				if((jsonData[q] == ',')||(jsonData[q] == '{')||(jsonData[q] == '}'))
				{
					keyFound = 0;
					pointlabel = 0;
				}
				else
					item.value[keyFound-4] = jsonData[q];
			}
		}			
	}
}

调用

Json_Decode((uint8_t*)"ABC");

起初本来是想用网上 CJSON 的源码,贯彻能复制黏贴的代码绝不自己写的原则,但是用CJSON源码我只能解析几次JSON数据。网上有人是调大堆栈,我试了下,也只能多解析几次JSON数据。

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值