JsonHashTable.cpp 1.3 KB

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