Introduction: ESP8266: Parsing JSON
Ter this instructable, wij will be using the ArduinoJson library for the ESP8266 to help us parse JSON gegevens and samenvatting values based on keys. The ArduinoJson library is also capable of serializing JSON, meaning you could generate your own JSON gegevens using gegevens from sensors connected to your ESP8266 or Arduino for example (will be covering more about JSON serialization, te detail, ter another instructable). So, let’s get embarked.
This project wasgoed done by mij, Nikhil Raghavendra, a Oorkonde te Rekentuig
Engineering student from Singapore Polytechnic, Schoolgebouw of Electrical and Electronic Engineering, under the guidance of my mentor Mr Teo Shin Jen.
Step 1: Install the ArduinoJson Library
Before you can use the ArduinoJson library, you have to make sure the library is installed on your rekentuig. To do a quick check, head overheen to the library manager te the Arduino IDE (Sketch ->, Include Library ->, Manage Libraries. ) and type “ArduinoJson” ter the text opbergruimte, if you see a green coloured “INSTALLED” label beside the name of the library, that means you are all good to go and you can proceed on to the next step. If you don’t see the label, click on the opbergruimte/division merienda and you will see the “Install” button. Kasstuk install and you are all set for the next step.
Step Two: Performing a GET Request
Before wij can embark parsing, wij need to have the JSON gegevens ter the very first place and to obtain our gegevens, wij perform a GET request. A GET request, spil the name suggests, gets the gegevens for us from a particular location using a specific URL. The boilerplate code to perform the GET request can be found below. For this example, wij will be performing a GET request using the http://jsonplaceholder.typicode.com/users/1 URL. You can call any API you like.
The gegevens that wij are going to parse is contained ter the payload variable. Wij don’t actually need this variable when wij are parsing our gegevens straks on.
Step Three: Using the ArduinoJson Assistant
The developers who developed the ArduinoJson library are so kleuter that they’ve even created an Assistant that writes the parser program for us using any JSON gegevens spil an input. To use the ArduinoJson assistant, you very first need to know how your JSON is formatted and to do that, type ter the URL that wij used to perform the GET request earlier on into the browser of your choice and klapper inject. Copy the JSON and head overheen to the ArduinoJson Assistant’s web pagina and paste it into the text opbergruimte below the label named “Input“. Then scroll down to take a look at the parsing program generated by the Assistant. Copy the entire program or just a section of it.
Step Four: Completing the Code and the Result
Copying and pasting the parsing program generated by the Assistant into the boilerplate code that wij used to perform a GET request earlier on would look like this:
Since wij are only interested ter the name, email and username of the user, wij just used a section of the parsing program generated by the assistant. You can use the serial preceptor to view the output. If you don’t see anything, press the reset button on your ESP8266 and *esplendor* you should see the output there. Note: The last line of code te the code above introduces a delay of 1 minute or 60,000 ms into the loop. This means that the API is only called merienda every minute. The number of times an API can be called within a specified timeframe varies and you are strongly encouraged to go after the guidelines specified by your API provider.