From e08fe6318ae0bcdccbd99e87a2b2cfc2befb0a07 Mon Sep 17 00:00:00 2001 From: Gabriele Bellini Date: Fri, 15 Jul 2016 00:50:06 +0200 Subject: [PATCH] Updated Bag of Tricks (markdown) --- Bag-of-Tricks.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/Bag-of-Tricks.md b/Bag-of-Tricks.md index ebb9313..5fc8b09 100644 --- a/Bag-of-Tricks.md +++ b/Bag-of-Tricks.md @@ -1,5 +1,31 @@ Here are helper class that can help you add missing features. +## Look for a nested key + +Suppose we've this json: + +```json +{"key1":1,"key2":{"subkey1":3,"subkey2":4}} +``` + +And we want to check presence of **subkey1** (or **subkey2**). Using **containsKey** method will not work, since **subkey1** and **subkey2** are nested keys inside **key2** value. This function allows to find key presence even if they are nested inside any key value: + +```c++ +bool containsNestedKey(const JsonObject& obj, const char* key) { + for (const JsonPair& pair : obj) { + if (!strcmp(pair.key, key)) + return true; + + if (containsNestedKey(pair.value.as(), key)) + return true; + } + + return false; +} +``` + +See issue [#322](https://github.com/bblanchon/ArduinoJson/issues/322) + ## Extract an array of integer Suppose we have an input like this: