如何进行字符串解析,并打印需要的字符串

来源:互联网 发布:sql清除注册表 编辑:程序博客网 时间:2024/05/29 19:08

一、应用场景。
需要进行字符解析的场景一般是走AT指令集的,比如GPS模块,GPRS模块,WIFI模块。下面给出一个WIFI模块接收到的数据包:

+IPD,260:{"results":[{"location":{"id":"WTTE97PU94T9","name":"Wuxi","country":"CN","path":"Wuxi,Wuxi,Jiangsu,China","timezone":"Asia/Shanghai","timezone_offset":"+08:00"},"now":{"text":"Cloudy","code":"4","temperature":"36"},"last_update":"2017-08-07T1

这是wifi模块从串口输出的数据。一般情况下,我们是用单片机的串口进行接收,并把它存在数据里。

二、解析过程。

 char *Weather_text_Point;char *Weather_temperature_Point;unsigned char *NET_DEVICE_GetIPD(unsigned short timeOut){char Param_out[1024]={0};#if(NET_DEVICE_TRANS == 0)    unsigned int  count = 0;    unsigned int byte = 0;    char sByte[5]={0};    char *ptrIPD;    unsigned char Weather_temperature_temp;#endif    do    {        if(NET_IO_WaitRecive() == REV_OK)                                       {            sprintf (Param_out ,"receive buf =%s\r\n", netIOInfo.buf);UsartPrintf(USART_DEBUG, Param_out);                #if(NET_DEVICE_TRANS == 0)            ptrIPD = strstr((char *)netIOInfo.buf, "IPD,");                         if(ptrIPD == NULL)                                                      {                UsartPrintf(USART_DEBUG, "\"IPD\" not found\r\n");            }            else            {                       ptrIPD = strstr(ptrIPD, "text");                ptrIPD=ptrIPD+7;                while(*ptrIPD !='"')                {                        Weather_text[count++]=*ptrIPD++ ;                }                Weather_text_Point=Weather_text ;                sprintf (Param_out ,"Weather_text =%s\r\n", Weather_text);                UsartPrintf(USART_DEBUG, Param_out);                count=0;                ptrIPD = strstr(ptrIPD, "temperature");                ptrIPD=ptrIPD+14;                while(*ptrIPD !='"')                {                        Weather_temperature[count++]=*ptrIPD++ ;                }                Weather_temperature_Point=Weather_temperature ;                sprintf (Param_out ,"Weather_temperature =%s\r\n", Weather_temperature);                UsartPrintf(USART_DEBUG, Param_out);                //Weather_Point=Weather_temperature ;//              while(*ptrIPD != ':')                                   //              {//                  sByte[count++] = *ptrIPD++;//              }//              byte = atoi(sByte);                     //            //  ptrIPD++;                                               //              for(count = 0; count < (byte+8); count++)                   //              {//                  netIOInfo.buf[count] = *ptrIPD++;//              }            //  memset(netIOInfo.buf + byte,0, sizeof(netIOInfo.buf) - byte);                               return netIOInfo.buf;                                               }#else            return netIOInfo.buf;#endif        }    } while(timeOut--);    return NULL;                                                        }

三、程序说明
1、首先利用strstr(str1,str2)函数,strstr是一种函数,从字符串str1中查找是否有符串str2,如果有,从str1中的str2位置起,返回str1的指针,如果没有,返回null。
程序中利用这个函数可以迅速定位到某些关键字。进而获取关键字后的信息。

2、利用while(*ptrIPD!=‘!’)这行代码,实现把特定的字符串写入相应的数组里。

3、利用sprintf()函数,进行打印。
int sprintf( char *buffer, const char *format, [ argument] … );
说明1:该函数包含在stdio.h的头文件中,使用时需要加入:
说明2:sprintf与printf函数的区别:二者功能相似,但是sprintf函数打印到字符串中,而printf函数打印输出到屏幕上。sprintf函数在我们完成其他数据类型转换成字符串类型的操作中应用广泛。此函数的精华在于可以格式化输出。
比如,程序中解析出来一组温度数据,放在一个数组里。那么想用串口把这个数据打印出来,就需要用到这个函数。

4、aoti可以把数组里的字符串转换成实际的数字。

阅读全文
0 0
原创粉丝点击