• python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Facebook Twitter Instagram
Devs Fixed
  • python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Devs Fixed
Home ยป Resolved: Custom JSON decoding: decode an array of different objects in several distinct arrays

Resolved: Custom JSON decoding: decode an array of different objects in several distinct arrays

0
By Isaac Tonny on 17/06/2022 Issue
Share
Facebook Twitter LinkedIn

Question:

I need you help to implement a custom JSON decoding. The JSON returned by the API is:
I don’t want to parse this JSON as an array of zones with no meaning. I’d like to produce a model with an array for every specific type of block, like this:
How can I implement the Decodable protocol (by using init(from decoder:)) to follow this logic? Thank you.

Answer:

This is a solution with nested containers. With the given (simplified but valid) JSON string
and the corresponding element structs
first decode the zones as nestedUnkeyedContainer then iterate the array and decode first the name key and depending on name the elements.
Side note: This way requires to declare the element arrays as variables.
Decoding the stuff is straightforward

If you have better answer, please add a comment about this, thank you!

decodable ios json swift
Share. Facebook Twitter LinkedIn

Related Posts

Resolved: Why when i change manualy the url on my react page it ignores the .css file?

24/03/2023

Resolved: navigate and redirect not redirecting to path in react router

24/03/2023

Resolved: Time Complexity: Find the number of people who know a secret leetcode 2327

24/03/2023

Leave A Reply

© 2023 DEVSFIX.COM

Type above and press Enter to search. Press Esc to cancel.