如何在arduino IDE中解析json数组

时间:2016-03-06 21:55:26

标签: php json arduino

我试图通过wifi解析Pi服务器上的JSON数组。我想解析以下内容:

  

[{ “设备”: “窗帘”, “值”: “1”},{ “设备”: “风扇”, “值”: “1”},{ “设备”: “窗帘”,”值 “:” 0 “},{” 设备 “:” 继电器”, “值”: “0”}]

这些内容是从网页上复制的:http://pi.access.com/states.php

以下是我的Arduino代码。

#include <ArduinoJson.h>

void setup() {
  Serial.begin(9600);
  while (!Serial) {
    // wait serial port initialization
  }

  StaticJsonBuffer<200> jsonBuffer;    

  char   json[] =
  "[{\"Device\":\"Curtain\",\"Value\":\"1\"},{\"Device\":\"Fan\",\"Value\":\"1\"}]";


  JsonArray& root = jsonBuffer.parseArray(json);

  // Test if parsing succeeds.
  if (!root.success()) {
    Serial.println("parseObject() failed");
    return;
  }

  // Fetch values.

  const char* device[] = root["Device"];
          int value[] = root["Value"];

  // Print values.
  Serial.println(device);
  Serial.println(value);

}

然后我收到了一堆错误......

JsonParseExample.ino: In function 'void setup()':
JsonParseExample:54: error: invalid conversion from 'const char*' to 'size_t {aka unsigned int}' [-fpermissive]
In file included from /Users/Wei0928/Documents/Arduino/libraries/ArduinoJson/src/../include/ArduinoJson/JsonArray.hpp:181:0,
                 from /Users/Wei0928/Documents/Arduino/libraries/ArduinoJson/src/../include/ArduinoJson.h:9,
                 from /Users/Wei0928/Documents/Arduino/libraries/ArduinoJson/src/ArduinoJson.h:14,
                 from JsonParseExample.ino:8:
/Users/Wei0928/Documents/Arduino/libraries/ArduinoJson/src/../include/ArduinoJson/JsonArray.ipp:16:27: error:   initializing argument 1 of 'ArduinoJson::JsonArraySubscript ArduinoJson::JsonArray::operator[](size_t)' [-fpermissive]
 inline JsonArraySubscript JsonArray::operator[](size_t index) {
                           ^
JsonParseExample:54: error: initializer fails to determine size of 'device'
JsonParseExample:54: error: array must be initialized with a brace-enclosed initializer
JsonParseExample:58: error: call of overloaded 'println(const char* [1])' is ambiguous
JsonParseExample.ino:58:24: note: candidates are:
In file included from /Users/Wei0928/Documents/Arduino/libraries/ArduinoJson/src/../include/ArduinoJson/Internals/../Internals/../Arduino/Print.hpp:87:0,
                 from /Users/Wei0928/Documents/Arduino/libraries/ArduinoJson/src/../include/ArduinoJson/Internals/../Internals/DummyPrint.hpp:10,
                 from /Users/Wei0928/Documents/Arduino/libraries/ArduinoJson/src/../include/ArduinoJson/Internals/../Internals/JsonPrintable.hpp:11,
                 from /Users/Wei0928/Documents/Arduino/libraries/ArduinoJson/src/../include/ArduinoJson/Internals/../JsonVariant.hpp:13,
                 from /Users/Wei0928/Documents/Arduino/libraries/ArduinoJson/src/../include/ArduinoJson/Internals/../JsonBuffer.hpp:15,
                 from /Users/Wei0928/Documents/Arduino/libraries/ArduinoJson/src/../include/ArduinoJson/Internals/BlockJsonBuffer.hpp:10,
                 from /Users/Wei0928/Documents/Arduino/libraries/ArduinoJson/src/../include/ArduinoJson/DynamicJsonBuffer.hpp:10,
                 from /Users/Wei0928/Documents/Arduino/libraries/ArduinoJson/src/../include/ArduinoJson.h:8,
                 from /Users/Wei0928/Documents/Arduino/libraries/ArduinoJson/src/ArduinoJson.h:14,
                 from JsonParseExample.ino:8:
/Applications/Arduino.app/Contents/Java/hardware/arduino/avr/cores/arduino/Print.h:73:12: note: size_t Print::println(char) <near match>
     size_t println(char);
            ^
/Applications/Arduino.app/Contents/Java/hardware/arduino/avr/cores/arduino/Print.h:73:12: note:   no known conversion for argument 1 from 'const char* [1]' to 'char'
/Applications/Arduino.app/Contents/Java/hardware/arduino/avr/cores/arduino/Print.h:74:12: note: size_t Print::println(unsigned char, int) <near match>
     size_t println(unsigned char, int = DEC);
            ^
/Applications/Arduino.app/Contents/Java/hardware/arduino/avr/cores/arduino/Print.h:74:12: note:   no known conversion for argument 1 from 'const char* [1]' to 'unsigned char'
/Applications/Arduino.app/Contents/Java/hardware/arduino/avr/cores/arduino/Print.h:75:12: note: size_t Print::println(int, int) <near match>
     size_t println(int, int = DEC);
            ^
/Applications/Arduino.app/Contents/Java/hardware/arduino/avr/cores/arduino/Print.h:75:12: note:   no known conversion for argument 1 from 'const char* [1]' to 'int'
/Applications/Arduino.app/Contents/Java/hardware/arduino/avr/cores/arduino/Print.h:76:12: note: size_t Print::println(unsigned int, int) <near match>
     size_t println(unsigned int, int = DEC);
            ^
/Applications/Arduino.app/Contents/Java/hardware/arduino/avr/cores/arduino/Print.h:76:12: note:   no known conversion for argument 1 from 'const char* [1]' to 'unsigned int'
/Applications/Arduino.app/Contents/Java/hardware/arduino/avr/cores/arduino/Print.h:77:12: note: size_t Print::println(long int, int) <near match>
     size_t println(long, int = DEC);
            ^
/Applications/Arduino.app/Contents/Java/hardware/arduino/avr/cores/arduino/Print.h:77:12: note:   no known conversion for argument 1 from 'const char* [1]' to 'long int'
/Applications/Arduino.app/Contents/Java/hardware/arduino/avr/cores/arduino/Print.h:78:12: note: size_t Print::println(long unsigned int, int) <near match>
     size_t println(unsigned long, int = DEC);
            ^
/Applications/Arduino.app/Contents/Java/hardware/arduino/avr/cores/arduino/Print.h:78:12: note:   no known conversion for argument 1 from 'const char* [1]' to 'long unsigned int'
JsonParseExample:59: error: 'value' was not declared in this scope
invalid conversion from 'const char*' to 'size_t {aka unsigned int}' [-fpermissive]

最重要的是,我还没有弄清楚如何单独读取数组。 任何建议都表示赞赏。

1 个答案:

答案 0 :(得分:0)

问题似乎在这里:

  const char* device[] = root["Device"];
          int value[] = root["Value"];

请注意,rootJsonArray不是 a JsonObject。因此,您无法使用字符串作为键来访问数组的元素。

试试这样:

JsonObject *firstDeviceObject = root[0];
const char* firstDeviceName = firstDeviceObject["Device"];