Im trying to parse JSON data from an RestAPI that gives me energy data for Norway (https://driftsdata.statnett.no/restapi/ProductionConsumption/GetLatestDetailedOverview)
<ProductionConsumptionOverviewViewModel xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/Statnett.Driftsdata.RestApi.Models">
<ConsumptionData>
...
</ConsumptionData>
<Headers>
...
</Headers>
<HydroData>
<ProdConsOverViewModelItem>
<style>hydro</style>
<textTranslationId>General.Hydro</textTranslationId>
<titleTranslationId i:nil="true"/>
<value/>
</ProdConsOverViewModelItem>
<ProdConsOverViewModelItem>
<style i:nil="true"/>
<textTranslationId i:nil="true"/>
<titleTranslationId>ProductionConsumption.HydroSEDesc</titleTranslationId>
<value>4 840</value>
I know it reads like XML but the documentation said JSON so here goes. Im having the same issues reading it as XML still so.
I manage to read the JSON response fine, but Im having trouble "digging down" to the correct spot since the identifiers are the same for the different regions. Let say I wanted the data for the Hydro production below (see screenshot). How would i get that? Ive tries setting the [titleTranslationId] == "ProductionConsumption.HydroSEDesc" but that didnt work.
It looks like XML but the documentation said JSON ? which is why im trying to treat it as JSON.
<ProductionConsumptionOverviewViewModel xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/Statnett.Driftsdata.RestApi.Models">
<ConsumptionData>
...
</ConsumptionData>
<Headers>
...
</Headers>
<HydroData>
<ProdConsOverViewModelItem>
<style>hydro</style>
<textTranslationId>General.Hydro</textTranslationId>
<titleTranslationId i:nil="true"/>
<value/>
</ProdConsOverViewModelItem>
<ProdConsOverViewModelItem>
<style i:nil="true"/>
<textTranslationId i:nil="true"/>
<titleTranslationId>ProductionConsumption.HydroSEDesc</titleTranslationId>
<value>4 840</value>
Im using SwiftyJSON to handle the response.
I found some other treads but couldnt get it to work for me. Anyone able to help? Heres my code:
func getEnergyData(url: String){
AF.request(url, method: .get).responseJSON{ response in
switch response.result {
case .success(let json):
print("json success")
//print(json)
let energyJSON : JSON = JSON(response.result)
self.updateEnergyData(json: energyJSON)
case .failure(let error):
print(error)
}
}
}
trying to parse it:
func updateEnergyData(json : JSON){
if let results = json["ProductionConsumptionOverviewViewModel"]["HydroData"]["ProdConsOverViewModelItem"]["value"].double{
print(results)
}
else{
print("parse fail")
}
}
}