Skip to main content
added 327 characters in body
Source Link
hcheung
  • 2k
  • 9
  • 16

String severity = myObject["features"]["severity"];

If you run a curl cli command on your PC to the url https://api.weather.gov/alerts/active?zone=OHC093, it returns a json object as shown below (with part of the elements removed to only highlight the structure):

{
    // ...

    "features": [
        {
            // ...

            "properties": {
                // ...

                "severity": "Severe",
                // ...
                "event": "Flood Warning"
                // ...
            },
        },
        
    ],

    // ...

}

Noticed that the features is an array element, so to access it, it is myObject["features"][0]. So theThe "severity" element is an element of "properties". The correct code isshould therefore be:

JsonDocument doc;

DeserializationError error = deserializeJson(doc, input);

JsonObject myObject = doc["features"][0];
JsonObject propertyObj = myObject["properties"];
const char* severity = propertyObj["severity"];
const char* event = propertyObj["event"];

BTW, ArduinoJson library offers online tool ArduinoJson Assistant for helping you to serialise and deserialise an JSON object.

If you run a curl to the url https://api.weather.gov/alerts/active?zone=OHC093, it returns a json object as shown below (with part of the elements removed to only highlight the structure):

{
    // ...

    "features": [
        {
            // ...

            "properties": {
                // ...

                "severity": "Severe",
                // ...
                "event": "Flood Warning"
                // ...
            },
        },
        
    ],

    // ...

}

Noticed that the features is an array element, so to access it, it is myObject["features"][0]. So the correct code is:

JsonDocument doc;

DeserializationError error = deserializeJson(doc, input);

JsonObject myObject = doc["features"][0];
JsonObject propertyObj = myObject["properties"];
const char* severity = propertyObj["severity"];
const char* event = propertyObj["event"];

String severity = myObject["features"]["severity"];

If you run a curl cli command on your PC to the url https://api.weather.gov/alerts/active?zone=OHC093, it returns a json object as shown below (with part of the elements removed to only highlight the structure):

{
    // ...

    "features": [
        {
            // ...

            "properties": {
                // ...

                "severity": "Severe",
                // ...
                "event": "Flood Warning"
                // ...
            },
        },
        
    ],

    // ...

}

Noticed that the features is an array element, so to access it, it is myObject["features"][0]. The "severity" element is an element of "properties". The correct code should therefore be:

JsonDocument doc;

DeserializationError error = deserializeJson(doc, input);

JsonObject myObject = doc["features"][0];
JsonObject propertyObj = myObject["properties"];
const char* severity = propertyObj["severity"];
const char* event = propertyObj["event"];

BTW, ArduinoJson library offers online tool ArduinoJson Assistant for helping you to serialise and deserialise an JSON object.

Source Link
hcheung
  • 2k
  • 9
  • 16

If you run a curl to the url https://api.weather.gov/alerts/active?zone=OHC093, it returns a json object as shown below (with part of the elements removed to only highlight the structure):

{
    // ...

    "features": [
        {
            // ...

            "properties": {
                // ...

                "severity": "Severe",
                // ...
                "event": "Flood Warning"
                // ...
            },
        },
        
    ],

    // ...

}

Noticed that the features is an array element, so to access it, it is myObject["features"][0]. So the correct code is:

JsonDocument doc;

DeserializationError error = deserializeJson(doc, input);

JsonObject myObject = doc["features"][0];
JsonObject propertyObj = myObject["properties"];
const char* severity = propertyObj["severity"];
const char* event = propertyObj["event"];