如何在arduino IDE中解析json数组

I am trying to parse a JSON array on a Pi server via wifi. I want to parse the following:

[{"Device":"Curtain","Value":"1"},{"Device":"Fan","Value":"1"},{"Device":"Blinds","Value":"0"},{"Device":"Relay","Value":"0"}]

Those are copied from the webpage:http://pi.access.com/states.php

the following are my Arduino code.

#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);

}

Then I got a bunch of errors...

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]

Top of that I still haven't figured out how to read the array individually. Any advice is appreciated.

1个回答



问题似乎在这里:</ p>

  const char * device [] =  root [“Device”]; 
int value [] = root [“Value”];
</ code> </ pre>

请注意 root </ code>是 JsonArray </ code>, not </ em> JsonObject </ code>。 因此,您无法使用字符串作为键来访问数组的元素。 </ p>

尝试这样:</ p>

  JsonObject * firstDeviceObject = root [0]; 
const char * firstDeviceName = firstDeviceObject [“Device” ];
</ code> </ pre>
</ div>

展开原文

原文

The problem seems to be here:

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

Note that root is a JsonArray, not a JsonObject. Hence, you cannot access the array's element with strings as keys.

Try it like this:

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

dongyan7950
dongyan7950 感谢您的意见。 但是,在修改之后,显示以下错误:数组下标的无效类型'ArduinoJson :: JsonObject * [const char [7]]'并且标记了这行代码。 'const char * firstDeviceName = firstDeviceObject [“Device”];' 我想我理解你的想法。 首先从数组中获取一个对象,然后从所述对象中获取我需要的项目。 我也试过了
4 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐