一般单片机处理cJSON格式的数据都直接使用cJSON库,但对于Ram较小的单片机,由于资源有限,这并不合适,但我们可以根据cJSON数据的特定格式,使用土方法,直接对字符进行查找裁剪即可
//截取字符串str中字符a与字符b间的子字符串到dest中,Num为从第num个字符a后开始截取
bit substr(u8* str,u8* dest,u8 num,char a,char b)
{
u16 i=0,j=0,count=0;
while(1){
if(i > Uart3_RX_SIZE-1) return 0; //防止边界溢出
if(str[i] == a) count++;
if(count != num) i++;
else break; //找到num个a
}
while(1)
{
if(i+j+1 > Uart3_RX_SIZE-1) return 0;
if(str[i+j+1] == b) break; //找到b
j++;
}
memcpy(dest,&str[i+1],j);
dest[j]='\0';
return 1;
}