gpt4 book ai didi

c - 如何在C中读取特定关键字的json数据包?

转载 作者:行者123 更新时间:2023-11-30 14:39:15 25 4
gpt4 key购买 nike

这是我的 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);

}

关于c - 如何在C中读取特定关键字的json数据包?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56169683/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com