【发布时间】:2016-04-02 13:26:51
【问题描述】:
我正在使用 Arduino 和 Open Weather Map API 创建一个气象站,但我在将响应解析为 sscanf 有用的东西时遇到了严重的麻烦。
这是一个响应示例:
{"coord":{"lon":-0.13,"lat":51.51},"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01d"}],"base":"cmc stations","main":{"temp":14.17,"pressure":1012,"humidity":74,"temp_min":13,"temp_max":15.8},"wind":{"speed":4.6,"deg":150},"clouds":{"all":0},"dt":1459602835,"sys":{"type":1,"id":5091,"message":0.0059,"country":"GB","sunrise":1459575095,"sunset":1459622222},"id":2643743,"name":"London","cod":200}
我想从以下位置解析天气信息(清除):
"weather":[{"id":800,"main":"Clear",
而临时信息 (14) 来自:
"main":{"temp":14.17,
这是代码,我正在使用:
if (character == '}') { // Just a delimiter
if (strstr(response, "\"weather\":[{")) { // to confirm that the string was found
sscanf(response, ",main\":%s,", weather);
Serial.printfn("\r\nfound weather = %s"), weather;
}
else if (strstr(response, "\"main\":{\"temp\":")) { // to confirm that the string was found
sscanf(response, "temp\":%2s,", temp);
Serial.printfn("\r\nfound temp = %s"), temp;
}
memset(response, 0, sizeof(response));
idx = 0;
}
但 sscanf 甚至无法正常工作,因为它总是打印 32 字节长的整个天气/临时字符串。
found weather = ,"weather":[{"id":800,"main":"Clear","description":
found temp = ],"base":"cmc stations","main":{"temp":14.17,"pressure":1011,"humi
有人知道如何使用 sscanf 解析这些字符串吗?
【问题讨论】:
-
使用
%[^\"]表示“直到我读到\"” -
您有
sscanf_s可用吗?或者 Arduinos 是否存在 Boost.Spirit? -
is 'Serial.printfn("\r\nfound weather = %s"), weather;'正确的?不应该是 'Serial.printfn("\r\nfound weather = %s", weather);' ?
-
这是Demo。与@user3121023 所写的内容一起使用。
-
@12431234123412341234123 确实如此。这就是问题所在……该死,谢谢!!
标签: c arduino scanf double-quotes openweathermap