JsonObject.cpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /*
  2. * Arduino JSON library
  3. * Benoit Blanchon 2014 - MIT License
  4. */
  5. #include <string.h> // for strcmp()
  6. #include "JsonArray.h"
  7. #include "JsonObject.h"
  8. #include "JsonValue.h"
  9. using namespace ArduinoJson::Parser;
  10. DEPRECATED JsonArray JsonObject::getArray(const char* key)
  11. {
  12. return operator[](key);
  13. }
  14. /*
  15. * Returns the token for the value associated with the specified key
  16. */
  17. JsonValue JsonObject::operator[](const char* desiredKey)
  18. {
  19. // sanity check
  20. if (desiredKey == 0 || !isObject())
  21. return null();
  22. // skip first token, it's the whole object
  23. JsonToken runningToken = firstChild();
  24. // scan each keys
  25. for (int i = 0; i < size() / 2; i++)
  26. {
  27. // get 'key' token string
  28. char* key = runningToken.getText();
  29. // move to the 'value' token
  30. runningToken = runningToken.nextSibling();
  31. // compare with desired name
  32. if (strcmp(desiredKey, key) == 0)
  33. {
  34. // return the value token that follows the key token
  35. return runningToken;
  36. }
  37. // skip nested tokens
  38. runningToken = runningToken.nextSibling();
  39. }
  40. // nothing found, return NULL
  41. return null();
  42. }