evermeer / EVReflection

Reflection based (Dictionary, CKRecord, NSManagedObject, Realm, JSON and XML) object mapping with extensions for Alamofire and Moya with RxSwift or ReactiveSwift
Other
965 stars 119 forks source link

parse json to Dictionary and nested Dictionaries #306

Closed tobyworks closed 5 years ago

tobyworks commented 5 years ago

I have the following json for example

Type.json

{  
   "type":{  
      "key1":"value1",
      "key2":"value2"
... 100+ more key values
   },
   "filter":{  
      "key1":{  
         "option1":"value1"
      },
      "key2":{  
         "option2":"value2"
      }
... 10+ more
   }
}

Is it possible to parse this to an object like:

class TypeOutput {
 var type : Dictionary<String,String>
 var filter: Dictionary<String,Dictionary<String,String>>
}

I'm coming from Android dev and GSON. In json this is super easy to get but not sure how to get it working on iOS

I can't parse this manually putting all keys in the object. Since this object is dynamic from the server. And will always contain different keys and values, The only known factor in this all is the root keys.

type and filter

tobyworks commented 5 years ago

struct SearchFilter : Codable {

var version : String
var environment : String
var type: [String: String]
var dropdowns : [String: [String: [String: String]]]
var dropdownsOrdering: [String: [Int: String]]
var dropdownTitles: [String: String]
var options : [String: [String: String]]

}

Decided to use codable instead

evermeer commented 5 years ago

@tobybonhof Tip: you can make using Codable even easier with https://github.com/evermeer/Stuff#codable