作者:1983king | 更新时间:2019-12-25 | 浏览量:1463
这段是从串口接收到的
{"M":"say","ID":"U123","S":"D999","C":"{\"n\":\"SetBrightness\",\"a\":\"brightness\",\"v\":\"50\"}","SIGN":"Aligenie","T":"1577109735"}
可以从 C 提取出 v 的值吗?
重要代码如下
void serialEvent()
{
if (Serial.available()>0)
{
String inputString = Serial.readStringUntil('\n');
inputString.trim();
Serial.println(inputString);
int len = inputString.length()+1;
if(inputString.startsWith("{") && inputString.endsWith("}"))
{
char jsonString[len];
inputString.toCharArray(jsonString,len);
aJsonObject *msg = aJson.parse(jsonString);
processMessage(msg);
aJson.deleteItem(msg);
}
}
- --- -- - ----- ------------------------------------------
void processMessage(aJsonObject *msg){
aJsonObject* method = aJson.getObjectItem(msg, "M");
aJsonObject* content = aJson.getObjectItem(msg, "C");
aJsonObject* contentV = aJson.getObjectItem(content, "v");
if (!method)
{
return;
}
String M=method->valuestring;
String C=content->valuestring;
String V=contentV->valuestring;
if(M=="say")
{
if(V == "50" )
{
Serial.println(V);
}
}
}