| 1 | // ArduinoJson - https://arduinojson.org |
| 2 | // Copyright © 2014-2026, Benoit BLANCHON |
| 3 | // MIT License |
| 4 | // |
| 5 | // This example shows how to parse a JSON document in an HTTP response. |
| 6 | // It uses the Ethernet library, but can be easily adapted for Wifi. |
| 7 | // |
| 8 | // It performs a GET resquest on https://arduinojson.org/example.json |
| 9 | // Here is the expected response: |
| 10 | // { |
| 11 | // "sensor": "gps", |
| 12 | // "time": 1351824120, |
| 13 | // "data": [ |
| 14 | // 48.756080, |
| 15 | // 2.302038 |
| 16 | // ] |
| 17 | // } |
| 18 | // |
| 19 | // https://arduinojson.org/v7/example/http-client/ |
| 20 | |
| 21 | #include <ArduinoJson.h> |
| 22 | #include <Ethernet.h> |
| 23 | #include <SPI.h> |
| 24 | |
| 25 | void setup() { |
| 26 | // Initialize Serial port |
| 27 | Serial.begin(9600); |
| 28 | while (!Serial) |
| 29 | continue; |
| 30 | |
| 31 | // Initialize Ethernet library |
| 32 | byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; |
| 33 | if (!Ethernet.begin(mac)) { |
| 34 | Serial.println(F("Failed to configure Ethernet")); |
| 35 | return; |
| 36 | } |
| 37 | delay(1000); |
| 38 | |
| 39 | Serial.println(F("Connecting...")); |
| 40 | |
| 41 | // Connect to HTTP server |
| 42 | EthernetClient client; |
| 43 | client.setTimeout(10000); |
| 44 | if (!client.connect("arduinojson.org", 80)) { |
| 45 | Serial.println(F("Connection failed")); |
| 46 | return; |
| 47 | } |
| 48 | |
| 49 | Serial.println(F("Connected!")); |
| 50 | |
| 51 | // Send HTTP request |
| 52 | client.println(F("GET /example.json HTTP/1.0")); |
| 53 | client.println(F("Host: arduinojson.org")); |
| 54 | client.println(F("Connection: close")); |
| 55 | if (client.println() == 0) { |
| 56 | Serial.println(F("Failed to send request")); |
| 57 | client.stop(); |
| 58 | return; |
| 59 | } |
| 60 | |
| 61 | // Check HTTP status |
| 62 | char status[32] = {0}; |
| 63 | client.readBytesUntil('\r', status, sizeof(status)); |
| 64 | // It should be "HTTP/1.0 200 OK" or "HTTP/1.1 200 OK" |
| 65 | if (strcmp(status + 9, "200 OK") != 0) { |
| 66 | Serial.print(F("Unexpected response: ")); |
| 67 | Serial.println(status); |
| 68 | client.stop(); |
| 69 | return; |
| 70 | } |
| 71 | |
| 72 | // Skip HTTP headers |
| 73 | char endOfHeaders[] = "\r\n\r\n"; |
| 74 | if (!client.find(endOfHeaders)) { |
| 75 | Serial.println(F("Invalid response")); |
| 76 | client.stop(); |
| 77 | return; |
| 78 | } |
| 79 | |
| 80 | // Allocate the JSON document |
| 81 | JsonDocument doc; |
| 82 | |
| 83 | // Parse JSON object |
| 84 | DeserializationError error = deserializeJson(doc, client); |
| 85 | if (error) { |
| 86 | Serial.print(F("deserializeJson() failed: ")); |
| 87 | Serial.println(error.f_str()); |
| 88 | client.stop(); |
| 89 | return; |
| 90 | } |
| 91 | |
| 92 | // Extract values |
| 93 | Serial.println(F("Response:")); |
| 94 | Serial.println(doc["sensor"].as<const char*>()); |
| 95 | Serial.println(doc["time"].as<long>()); |
| 96 | Serial.println(doc["data"][0].as<float>(), 6); |
| 97 | Serial.println(doc["data"][1].as<float>(), 6); |
| 98 | |
| 99 | // Disconnect |
| 100 | client.stop(); |
| 101 | } |
| 102 | |
| 103 | void loop() { |
| 104 | // not used in this example |
| 105 | } |
| 106 | |
| 107 | // Performance issue? |
| 108 | // ------------------ |
| 109 | // |
| 110 | // EthernetClient is an unbuffered stream, which is not optimal for ArduinoJson. |
| 111 | // See: https://arduinojson.org/v7/how-to/improve-speed/ |
| 112 | |
| 113 | // See also |
| 114 | // -------- |
| 115 | // |
| 116 | // https://arduinojson.org/ contains the documentation for all the functions |
| 117 | // used above. It also includes an FAQ that will help you solve any |
| 118 | // serialization problem. |
| 119 | // |
| 120 | // The book "Mastering ArduinoJson" contains a tutorial on deserialization |
| 121 | // showing how to parse the response from GitHub's API. In the last chapter, |
| 122 | // it shows how to parse the huge documents from OpenWeatherMap |
| 123 | // and Reddit. |
| 124 | // Learn more at https://arduinojson.org/book/ |
| 125 | // Use the coupon code TWENTY for a 20% discount ❤❤❤❤❤ |