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.
As an aside, I'm not sure which JSON parsing API you're using. The example above uses the Codable APIs. The general approach should be the same either way: find the array that contains the different consumption values and filter it to get just the value for the region you're looking for.
I wrote the above using Xcode 12 beta, so there might be a few things to adapt if you're using an earlier version. You might need to import Combine, add .assertNoFailure() after the .decode line, and revert to the trailing closure syntax before multiple trailing closures:
.sink(receiveCompletion: { _ in }) { energyResponses in
That error is related to the lifetime of the cancelHandle constant. I wrote the example in a blank macOS playground. If, for example, you declared these variables in a button's tap handler, you'll need to extract the declaration of cancelHandle to a broader scope than the handler's closure. You could change it to be a global variable:
var cancelHandle: AnyCancellable? = nil
…then assign it to the return value of sink when you make the network call.
If we go back to Alamofire to simplify the example, you can achieve parse your response with the type @krilnon described:
AF.request(url).responseDecodable(of: EnergyResponse.self) { response in
// You can inspect all of the response properties, including the Result of parsing it into EnergyResponse.
}
You can then use the techniques described to get the data you want. If you want to use Combine, you can use Alamofire's integration:
The .value() operator takes the DataResponse you'd normally get from Alamofire and breaks it into a value stream, like you'd get from the previous Combine examples. Also, like those examples, you need subscribe through sink or another operator and handle the cancellation token. However, I would recommend starting with the simpler completion handler and only moving to the Combine-based solution if you're going to be using Combine.