c - 如何读取 C 中特定关键字的 json 数据包?
问题描述
这是我的 json 数据包,我想阅读“14.469121”:
{"jsonrpc": "2.0", "method": "notifySpeed", "params": {"speed": "14.469121"}}
我尝试了一些在线解决方案并实现了一些逻辑。
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);
}
}
}
我想读取速度数据。
解决方案
谢谢大家的帮助,终于我成功实施了。
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);
}
推荐阅读
- laravel - 如何在 laravel 刀片模板中显示消息包对象?
- protractor - 量角器进程退出,错误代码 100
- python - 如何将 pandas df 写入具有多个空格的文本
- azure-service-fabric - Service Fabric Explorer 运行状况未知
- svg - svg viewBox 奇怪的行为?
- jenkins - Bitbucket 团队/项目 - 在管道 Jenkinsfile 中获取 BITBUCKET_PAYLOAD
- node.js - 在用户输入上触发电报机器人命令
- spring-boot - 尝试从 docker 上发布的 Neo4jRepository 检索数据时出现 Neo4jSession 错误[ONLY DOCKER ISSUE]
- ios - 将 UIView/UIGestureRecognizer 添加到 UIPresentationController 中的呈现视图
- tensorflow - 如何在云中部署实时学习张量流模型?