请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。
器件型号:EK-TM4C1294XL 工具与软件:
在相关问题中、我遇到了 TI json.c example/template 中包含的函数 GetFieldValueString 的问题。 我们最终发现了与双引号的初始搜索相关的问题。 根据 JSON 在发送端的结构、它可能会在引号前添加字符(空格、制表符、返回)"。 这将使第一个 if 语句失效。 这似乎发生了,甚至当我试图消除所有的空间,返回,等等发送端。 因此、我对代码做了细微修改。 指针将移动到下一个字符、直到找到引号、而不是返回-1并且如果在开头找不到双引号、则失败。 在它放弃之前我包含了20个字符的任意限制。 如果找不到双引号、这应该可以防止无限循环或溢出。
配置 SDK 代码。 我已经测试了它,它似乎起作用(至少对我的目标。
/****************************************************************************** * * This function searches for a value in a JSON item as quoted value. These * values are quoted values so if the number is not a quoted value this * function returns -1. If the quoted value is found then it is returned in * the pcDataDest array. * *****************************************************************************/ int32_t GetFieldValueString(tBufPtr *psBufPtr, char *pcDataDest, uint32_t ui32SizeDest) { int32_t i32OutIdx; uint8_t Idx_counter = 0; /* Counter to prevent infinite loop. */ while (Idx_counter < 20) { /* String value starts with double quote " but this may not always be the * first character. Look for " and then move on. */ if (BufData8Get(psBufPtr) == '"') { /*Found start of the string with double quote. Reset the counter. Move * to the next step*/ Idx_counter = 0; break; } else { /*move pointer to next character. Searching for double quote mark.*/ BufPtrInc(psBufPtr, 1); /* Fail safe against infinite loop. */ Idx_counter++; } } /* Skip the initial " char. */ if (BufPtrInc(psBufPtr, 1) != 1) { return(-1); } for (i32OutIdx = 0; i32OutIdx < ui32SizeDest;) { /* Either a '}', ',', or '"' ends an item. */ if ((BufData8Get(psBufPtr) == '}') || (BufData8Get(psBufPtr) == ',') || (BufData8Get(psBufPtr) == '"')) { /* Null terminate the string and return. */ pcDataDest[i32OutIdx] = 0; return(i32OutIdx); } /* Continue copying chars into the destination buffer. */ pcDataDest[i32OutIdx] = BufData8Get(psBufPtr); /* These can occur in the response string and need to be ignored. */ if ((pcDataDest[i32OutIdx] == '\r') || (pcDataDest[i32OutIdx] == '\n')) { pcDataDest[i32OutIdx] = 0; } else { i32OutIdx++; } if (BufPtrInc(psBufPtr, 1) != 1) { break; } } /* Make sure to null terminate inside the current string. */ if (i32OutIdx == ui32SizeDest) { pcDataDest[i32OutIdx - 1] = 0; return(i32OutIdx - 1); } return(-1); }
---
Devon