Search
  • Meghan Gill

Structs in JSON

What is a struct?


A struct is a customizable data structure. It can contain properties, classes, objects or other structs. People use structs because it makes data more manageable. It puts all the properties into one container (a struct) so you do not repeat code.



Where can we find structs?


Structs are commonly used in C programming languages, such as C, C++ and C#. Structs are used when you want a small data structure that contains data that is usually not changed after the struct is created. For more complex behavior, classes are used.


Structs do not exist in Kotlin or Java. Instead we often parse a struct’s data into a class. As an Android developer it is important to be able to recognize structs and know how to handle them. Specifically when parsing data using JSON(JavaScript Object Notation), such as when accessing data from a web API. The example below is from the Accuweather API, 1 Day of Daily Forecasts.

How do I recognize a struct in a JSON file?


A struct can have different data types, unlike a list or array in languages like Java and Kotlin. It can also contain other structs.


{

/* This JSON file is from the Accuweather API, 1 Day of Daily Forecasts. It contains two elements 1) the struct "Headline" and 2) the array "DailyForecasts" which contains structs.

*/

"Headline": { //This struct holds string-value pairs, AKA key-value pairs. "EffectiveDate": "2021-03-18T11:00:00-04:00", "EffectiveEpochDate": 1616079600, "Severity": 3, "Text": "Rain from late this morning to late tonight, when it will mix with snow", "Category": "snow/rain", "EndDate": "2021-03-19T08:00:00-04:00", "EndEpochDate": 1616155200, "MobileLink": "http://m.accuweather.com/en/us/ridgewood- ny/11385/extended-weather-forecast/329638?lang=en-us", "Link": "http://www.accuweather.com/en/us/ridgewood- ny/11385/daily-weather-forecast/329638?lang=en-us" }, "DailyForecasts": [ //This array has one element {

/* The struct begins after the open bracket. It contains string-value pairs, other structs and an array.

*/ "Date": "2021-03-18T07:00:00-04:00", "EpochDate": 1616065200, "Temperature": {

//This is a struct that holds two structs: "Minimum" and "Maximum" "Minimum": { "Value": 33, "Unit": "F", "UnitType": 18 }, "Maximum": { "Value": 50, "Unit": "F", "UnitType": 18 } }, "Day": { //Struct "Icon": 18, "IconPhrase": "Rain", "HasPrecipitation": true, "PrecipitationType": "Rain", "PrecipitationIntensity": "Heavy" }, "Night": { //Struct "Icon": 29, "IconPhrase": "Rain and snow", "HasPrecipitation": true, "PrecipitationType": "Mixed", "PrecipitationIntensity": "Heavy" }, "Sources": [ //Array "AccuWeather" ], "MobileLink": "http://m.accuweather.com/en/us/ridgewood-ny/11385/daily-weather-forecast/329638?day=1&lang=en-us", //String-value pair "Link": "http://www.accuweather.com/en/us/ridgewood-ny/11385/daily-weather-forecast/329638?day=1&lang=en-us" //String-value pair } //Struct ends ] //Closing bracket for array }




15 views0 comments

© 2023 by Copywriter CV. Proudly created with Wix.com

Follow me

  • Twitter
  • linkedin-square