Reputation: 19
This my json packet and I want to read "14.469121":
{"jsonrpc": "2.0", "method": "notifySpeed", "params": {"speed": "14.469121"}}
I tried some online solutions and implemented some logic.
ptr = strtok(parse_recData,", ");
while(ptr != NULL)
{
countTillMethod--;
if(countTillMethod == 0)
{
if(strcmp(ptr,"\"notifySpeed\"")==0)
{
if(!(strcmp(ptr,"\"Speed\"" )))
Speed = strtok(NULL,", ");
SpeedValue = atoi (Speed);
if (SpeedValue > PERMISSIBLE_LIMIT)
touchControl (DISABLE);
else
touchControl (ENABLE);
}
}
}
I want to read speed data.
Upvotes: 0
Views: 173
Reputation: 19
char parse_recData[215] = {"jsonrpc": "2.0", "method": "notifySpeed", "params": {"speed": "14.469121"}};
char parse_recData_backup[215];
char *ptr1 = NULL;
int Speed = 0;
strcpy(parse_recData_backup, parse_recData);
ptr = strtok(parse_recData,", ");
while(ptr != NULL)
{
countTillMethod--;
if(countTillMethod == 0)
{
if(strcmp(ptr,"\"notifySpeed\"")==0)
{
syslog(LOG_INFO,"Received Speed\n");
ptr1 = strstr(parse_recData_backup, "\"speed\": ");
ptr1 += strlen("\"speed\": ") + 1;
/* Converts the string to integer */
Speed = atoi(ptr1);
syslog(LOG_INFO," speed is %d \r\n", Speed);
/* Compare the speed with permissiable limit */
if (Speed > PERMISSIBLE_LIMIT)
touchControl (DISABLE);
else
touchControl (ENABLE);
}
}
ptr = strtok(NULL, ",");
}
Upvotes: 0
Reputation: 11
Thank you everyone help, finally i implement successfully.
else if(strcmp(ptr,"\"notifySpeed\"")==0)
{
syslog(LOG_INFO,"Received Speed\n");
ptr1 = strstr(parse_recData_backup, "\"params\"");
ptr1 += strlen("params");
ptr1 = strstr(parse_recData_backup, "\"speed\": ");
ptr1 += strlen("\"speed\": ") + 1;
/* get the exect value */
for(i=0; ptr[i]!='\0'; ++i)
{
while (!((ptr1[i]>='0'&&ptr1[i]<='9') || (ptr1[i] == '.') || (ptr1[i] == '\0')))
{
for(j=i;ptr1[j]!='\0';++j)
{
ptr1[j]=ptr1[j+1];
}
ptr1[j]='\0';
}
}
syslog(LOG_INFO," %s \r\n", ptr1);
/* Converts the string to integer */
Speed = atoi(ptr1);
syslog(LOG_INFO," speed is %d \r\n", Speed);
/* Compare the speed with permissiable limit */
if (Speed > PERMISSIBLE_LIMIT)
touchControl (DISABLE);
else
touchControl (ENABLE);
}
Upvotes: 1