Parsowanie odpowiedzi JSON
Wyodrębniać pola za pomocą ArduinoJson.
Parsowanie odpowiedzi JSON to bezpłatna lekcja Arduino & IoT Academy na CoddyKit. To lekcja 3 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Arduino & IoT Academy, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Arduino & IoT Academy zawiera 4 lekcji w sumie.
Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.
Responses Are Just Text
A server reply arrives as one long string. To use a value inside it, you first have to pull that value out cleanly.
Why Not Search by Hand
Hunting through text with indexOf is fragile. A real JSON parser understands the structure and gives you fields safely.
Meet ArduinoJson
The ArduinoJson library turns a JSON string into objects you can read by key. Install it from the Library Manager.
#include <ArduinoJson.h>Make a Document
Create a JsonDocument to hold the parsed data. It is the container ArduinoJson fills in for you.
JsonDocument doc;Parse the String
Call deserializeJson() with your document and the response text. This reads the string and fills the document.
deserializeJson(doc, body);Check for Errors
deserializeJson returns an error you should test. Bad or partial JSON should be caught before you trust any field.
if (deserializeJson(doc, body)) {
return; // parse failed
}Read a Field by Key
Access a value with its key, just like a dictionary. Tell ArduinoJson the type you expect for that field.
float temp = doc["temp"];Read Nested Values
For nested JSON, chain the keys. Each bracket steps one level deeper into the structure to reach the value you want.
int id = doc["device"]["id"];Loop Through an Array
If a field is a JSON array, you can walk it with a for loop and read each element in turn.
for (JsonVariant v : doc["list"].as<JsonArray>()) {
}Size the Document Right
A JsonDocument uses memory. Keep it just big enough for your data, since RAM on a microcontroller is limited.
Strings Need Care
Treat a parsed string as valid only while the source text still exists. Copy it out if you need it to last longer.
Quick Check
Which function actually converts the JSON text into a readable document?
Recap
You used ArduinoJson to deserialize a response, checked for errors, and read fields and arrays by key. Raw text is now real data. 🎉
Często zadawane pytania
Czy lekcja „Parsowanie odpowiedzi JSON” jest bezpłatna?
Tak — pełny tekst „Parsowanie odpowiedzi JSON” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Arduino & IoT Academy, przejdź na CoddyKit PRO. Kurs Arduino & IoT Academy zawiera 4 lekcji w sumie.
Co nauczysz się w „Parsowanie odpowiedzi JSON”?
Wyodrębniać pola za pomocą ArduinoJson. Ćwiczysz Arduino & IoT Academy z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.
Czy potrzebuję doświadczenia, aby zacząć Arduino & IoT Academy?
Nie wymagamy żadnego doświadczenia. Arduino & IoT Academy w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 3 z 4.
Ile czasu zajmuje lekcja „Parsowanie odpowiedzi JSON”?
Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.
Czy mogę pisać i uruchamiać kod w tej lekcji Arduino & IoT Academy?
Tak. Każda lekcja Arduino & IoT Academy zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.
Wszystkie lekcje w tym kursie
- Pobieranie danych z API metodą GET
- Wysyłanie odczytów czujników metodą POST
- Parsowanie odpowiedzi JSON
- Bezpieczne używanie HTTPS