0

I have a custom class whose values are taken from the following data Structure

{
  key1 = value1 ;
  key2 = value2;
  ...}

I can handle this data structure . With another request, i have the following data structure :

{
 rootKey = {
   key1 = value1;
   key2 = value2;
   ..};`enter code here`
 }

i would like to parse it or use another method to get the result as in the first structure. Having a java background and little swift experience, i couldn t find a easy and fast way to do it.

it75
  • 125
  • 10
  • 1
    You need more detail in the question. Is this coming from JSON? Do you have actual class or struct definitions for either type made? – jnpdx Mar 15 '21 at 17:36
  • this is coming from datasnapshot in firebase . The first struct allows me to use a decoder to retrieve an instance of a custom class . From firebase requests , i couldn t have a precise answser just too loop again and again to have the mandatory values Since the elements to be removed always have the same strcuture , i would prefer just removing the unecessary data in order to have a clean solution instead of loops – it75 Mar 15 '21 at 17:58
  • I suggest you show some code – jnpdx Mar 15 '21 at 17:58
  • https://stackoverflow.com/questions/66630825/request-result-structure-firebasedatabase-swift – it75 Mar 15 '21 at 18:02

1 Answers1

0

If you use Decodable structs, you must define 2 data structures:

struct First: Decodable {
    let key1: String
    let key2: String
}

struct Second: Decodable {
    let rootKey: First
}

Then you can perform two different data requests, obtain different json Data objects. For the first case you try to decode First object:

func request1(completion: (Result<First, Error>) -> Void) {
    // make async request for data
    let data = Data()

    // decode result and call your completion handler in request completion
    do {
        let model = try JSONDecoder().decode(First.self, from: data)
        completion(.success(model))
    } catch {
        completion(.failure(error))
    }
}

For the second one, you try to decode the Second object, but take only the First object from rootKey:

func request2(completion: (Result<First, Error>) -> Void) {
    // make async request for data
    let data = Data()

    // decode result and call your completion handler in request completion
    do {
        let model = try JSONDecoder().decode(Second.self, from: data)
        // take the First object from decoded result
        completion(.success(model.rootKey))
    } catch {
        completion(.failure(error))
    }
}
loverap007
  • 129
  • 6
  • Hello, thank you . I m not sure i got it right for the second decodable . I need the first one only. In the description i made , i attached the link to the same problem but from a firebase point of view . I use firebase decode which is i guess the same kind of Json decoder . using firebase decoder or json decoder on the provided data, i get an error. Without the rootkey part, it works fine, i just don t know how to get rid of the rootkey from the data.Having several requests from a query that only takes one request in java is what i want to avoid – it75 Mar 15 '21 at 20:06