0Pricing
Arduino & IoT Academy · レッスン

JSONレスポンスを解析する

ArduinoJsonを使ってフィールドを取り出します。

「JSONレスポンスを解析する」はCoddyKit上の無料Arduino & IoT Academyレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはArduino & IoT Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Arduino & IoT Academyコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

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. 🎉

よくある質問

「JSONレスポンスを解析する」レッスンは無料ですか?

はい。「JSONレスポンスを解析する」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Arduino & IoT Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Arduino & IoT Academyコースには全4レッスンが含まれています。

「JSONレスポンスを解析する」で何を学びますか?

ArduinoJsonを使ってフィールドを取り出します。 ブラウザで直接実行するハンズオンコードでArduino & IoT Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Arduino & IoT Academyを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのArduino & IoT Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。

「JSONレスポンスを解析する」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このArduino & IoT Academyレッスンでコードを書いて実行できますか?

はい。すべてのArduino & IoT Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. APIからGETデータを取得する
  2. センサー値をPOSTする
  3. JSONレスポンスを解析する
  4. HTTPSを安全に使う
← Arduino & IoT Academyに戻る