How can i get to the inner part of a multilevelled JSON structure?

104 Views Asked by At

I'm trying to get some local weather data out of a JSON-file. If i had any hair when i started this it would be gone by now. It's so frustrating, but i can not seem to get my head around this. I read all about here at the stack, but i'm still stuck. No i'm trying to use XSuperObject but so far i have not succeeded in dissecting this, so please help me to understand. I have a lot of code from trying this, but i will provide my last attempt. Enclosed is also part of the JSON file, but it should be complete as it is. The rest of the files is just a lots of more "TimeSeries". My small test application is a form with a TMEmo for the JSON text and a TListBox for the result strings. Please can anyone set me on the right track to understand this JSON? How can i get to inner part of this structure? Thanks!

{
    "type": "Feature",
    "geometry": {
        "type": "Point",
        "coordinates": [
            11,
            60.74,
            237
        ]
    },
    "properties": {
        "meta": {
            "updated_at": "2023-01-05T13:36:26Z",
            "units": {
                "air_pressure_at_sea_level": "hPa",
                "air_temperature": "celsius",
                "cloud_area_fraction": "%",
                "precipitation_amount": "mm",
                "relative_humidity": "%",
                "wind_from_direction": "degrees",
                "wind_speed": "m\/s"
            }
        },
        "timeseries": [
            {
                "time": "2023-01-05T14:00:00Z",
                "data": {
                    "instant": {
                        "details": {
                            "air_pressure_at_sea_level": 1020.2,
                            "air_temperature": -4.2,
                            "cloud_area_fraction": 100.0,
                            "relative_humidity": 80.9,
                            "wind_from_direction": 72.1,
                            "wind_speed": 3.2
                        }
                    },
                    "next_12_hours": {
                        "summary": {
                            "symbol_code": "cloudy"
                        }
                    },
                    "next_1_hours": {
                        "summary": {
                            "symbol_code": "cloudy"
                        },
                        "details": {
                            "precipitation_amount": 0.0
                        }
                    },
                    "next_6_hours": {
                        "summary": {
                            "symbol_code": "cloudy"
                        },
                        "details": {
                            "precipitation_amount": 0.0
                        }
                    }
                }
            },
            {
                "time": "2023-01-05T15:00:00Z",
                "data": {
                    "instant": {
                        "details": {
                            "air_pressure_at_sea_level": 1020.7,
                            "air_temperature": -4.3,
                            "cloud_area_fraction": 100.0,
                            "relative_humidity": 84.1,
                            "wind_from_direction": 72.9,
                            "wind_speed": 3.5
                        }
                    },
                    "next_12_hours": {
                        "summary": {
                            "symbol_code": "lightsnow"
                        }
                    },
                    "next_1_hours": {
                        "summary": {
                            "symbol_code": "cloudy"
                        },
                        "details": {
                            "precipitation_amount": 0.0
                        }
                    },
                    "next_6_hours": {
                        "summary": {
                            "symbol_code": "cloudy"
                        },
                        "details": {
                            "precipitation_amount": 0.0
                        }
                    }
                }
            },
            {
                "time": "2023-01-05T16:00:00Z",
                "data": {
                    "instant": {
                        "details": {
                            "air_pressure_at_sea_level": 1020.9,
                            "air_temperature": -4.4,
                            "cloud_area_fraction": 99.9,
                            "relative_humidity": 87.0,
                            "wind_from_direction": 78.1,
                            "wind_speed": 4.2
                        }
                    },
                    "next_12_hours": {
                        "summary": {
                            "symbol_code": "lightsnow"
                        }
                    },
                    "next_1_hours": {
                        "summary": {
                            "symbol_code": "cloudy"
                        },
                        "details": {
                            "precipitation_amount": 0.0
                        }
                    },
                    "next_6_hours": {
                        "summary": {
                            "symbol_code": "cloudy"
                        },
                        "details": {
                            "precipitation_amount": 0.0
                        }
                    }
                }
            },
            {
                "time": "2023-01-05T17:00:00Z",
                "data": {
                    "instant": {
                        "details": {
                            "air_pressure_at_sea_level": 1021.1,
                            "air_temperature": -4.6,
                            "cloud_area_fraction": 99.7,
                            "relative_humidity": 88.9,
                            "wind_from_direction": 79.4,
                            "wind_speed": 4.5
                        }
                    },
                    "next_12_hours": {
                        "summary": {
                            "symbol_code": "lightsnow"
                        }
                    },
                    "next_1_hours": {
                        "summary": {
                            "symbol_code": "cloudy"
                        },
                        "details": {
                            "precipitation_amount": 0.0
                        }
                    },
                    "next_6_hours": {
                        "summary": {
                            "symbol_code": "cloudy"
                        },
                        "details": {
                            "precipitation_amount": 0.0
                        }
                    }
                }
            },
            {
                "time": "2023-01-05T18:00:00Z",
                "data": {
                    "instant": {
                        "details": {
                            "air_pressure_at_sea_level": 1021.7,
                            "air_temperature": -4.6,
                            "cloud_area_fraction": 100.0,
                            "relative_humidity": 89.1,
                            "wind_from_direction": 78.7,
                            "wind_speed": 4.4
                        }
                    },
                    "next_12_hours": {
                        "summary": {
                            "symbol_code": "lightsnow"
                        }
                    },
                    "next_1_hours": {
                        "summary": {
                            "symbol_code": "cloudy"
                        },
                        "details": {
                            "precipitation_amount": 0.0
                        }
                    },
                    "next_6_hours": {
                        "summary": {
                            "symbol_code": "cloudy"
                        },
                        "details": {
                            "precipitation_amount": 0.0
                        }
                    }
                }
            },
            {
                "time": "2023-01-05T19:00:00Z",
                "data": {
                    "instant": {
                        "details": {
                            "air_pressure_at_sea_level": 1021.8,
                            "air_temperature": -4.6,
                            "cloud_area_fraction": 100.0,
                            "relative_humidity": 89.6,
                            "wind_from_direction": 84.8,
                            "wind_speed": 4.6
                        }
                    },
                    "next_12_hours": {
                        "summary": {
                            "symbol_code": "lightsnow"
                        }
                    },
                    "next_1_hours": {
                        "summary": {
                            "symbol_code": "cloudy"
                        },
                        "details": {
                            "precipitation_amount": 0.0
                        }
                    },
                    "next_6_hours": {
                        "summary": {
                            "symbol_code": "cloudy"
                        },
                        "details": {
                            "precipitation_amount": 0.0
                        }
                    }
                }
            },
            {
                "time": "2023-01-05T20:00:00Z",
                "data": {
                    "instant": {
                        "details": {
                            "air_pressure_at_sea_level": 1022.0,
                            "air_temperature": -4.7,
                            "cloud_area_fraction": 100.0,
                            "relative_humidity": 89.0,
                            "wind_from_direction": 88.2,
                            "wind_speed": 4.6
                        }
                    },
                    "next_12_hours": {
                        "summary": {
                            "symbol_code": "lightsnow"
                        }
                    },
                    "next_1_hours": {
                        "summary": {
                            "symbol_code": "cloudy"
                        },
                        "details": {
                            "precipitation_amount": 0.0
                        }
                    },
                    "next_6_hours": {
                        "summary": {
                            "symbol_code": "cloudy"
                        },
                        "details": {
                            "precipitation_amount": 0.0
                        }
                    }
                }
            },
            {
                "time": "2023-01-05T21:00:00Z",
                "data": {
                    "instant": {
                        "details": {
                            "air_pressure_at_sea_level": 1022.1,
                            "air_temperature": -4.8,
                            "cloud_area_fraction": 100.0,
                            "relative_humidity": 87.0,
                            "wind_from_direction": 102.1,
                            "wind_speed": 4.4
                        }
                    },
                    "next_12_hours": {
                        "summary": {
                            "symbol_code": "lightsnow"
                        }
                    },
                    "next_1_hours": {
                        "summary": {
                            "symbol_code": "cloudy"
                        },
                        "details": {
                            "precipitation_amount": 0.0
                        }
                    },
                    "next_6_hours": {
                        "summary": {
                            "symbol_code": "lightsnow"
                        },
                        "details": {
                            "precipitation_amount": 0.6
                        }
                    }
                }
            }            
      }
}

procedure TfrmMain.Button1Click(Sender: TObject);
var
  obj: ISuperObject;
  arr: ISuperArray;
  itm: IMember;
begin
  obj := SO(memTxt.Lines.Text);
  arr := obj.O['timeseries'].O['data'].O['instant'].A['details'];
  for itm in arr do
  begin
    lbPairs.Items.Add(itm.AsObject.F['air_temperature'].ToString);
  end;
end;

1

There are 1 best solutions below

7
On

The mistake is that timeseries is an array, not details :

procedure TForm2.Button1Click(Sender: TObject);
var
  obj, o: ISuperObject;
  arr: ISuperArray;
  itm: ISuperObject;
  j: integer;
begin
  obj := SO(memTxt.Lines.Text);
  arr := obj.O['properties'].A['timeseries'];

  for j := 0 to arr.Length - 1 do
  begin
    lbPairs.Items.Add(arr.O[j].O['data'].O['instant'].O['details'].F['air_temperature'].ToString);
  end;
end;