import UIKit
class TwoViewController: UIViewControll
import UIKit
class TwoViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .white
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.test()
}
func test() {
let str = """
[ { "experiment_id" : "EXP_AB", "feature" : "mobile_new_payment_page", "groups" : { "test_group" : [ "00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20" ], "control_group" : [] } }, { "experiment_id" : "EXP_AB", "feature" : "hyperwallet_display_toggle_new", "groups" : { "test_group" : [ "20", "00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "16", "17", "18", "19", "15" ], "control_group" : [] } }, { "experiment_id" : "EXP_AB", "feature" : "payment_top_up_remove_10_experiment", "groups" : { "test_group" : [ "00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10" ], "control_group" : [ "11", "12", "13", "14", "15", "16", "17", "18", "19", "20" ] } }, { "experiment_id" : "EXP_AB", "feature" : "language_challenge_toggle", "groups" : { "test_group" : [ "00" ], "control_group" : [] } }, { "experiment_id" : "EXP_AB", "feature" : "onboarding_mobile_toggle", "groups" : { "test_group" : [ "00", "01", "03" ], "control_group" : [] } }, { "experiment_id" : "EXP_AB", "feature" : "lesson_invitation_package_toggle", "groups" : { "test_group" : [ "00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20" ], "control_group" : [] } }, { "experiment_id" : "EXP_AB", "feature" : "classroom_pip_model_enable", "groups" : { "test_group" : [ "00" ], "control_group" : [ "20", "00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "16", "17", "18", "19", "15" ] } } ]
"""
if let object: [RootClass] = MGHelper<[RootClass]>.transFormObject(jsonString: str) {
let jsonStr = object.array?.json
print(jsonStr)
UserDefaults.standard.set(jsonStr, forKey: "HHHH")
UserDefaults.standard.synchronize()
DispatchQueue.main.asyncAfter(deadline: .now()+0.5) {
let userStr = UserDefaults.standard.value(forKey: "HHHH")
print(userStr)
if let object1: [RootClass] = MGHelper<[RootClass]>.transFormObject(jsonString: userStr as! String) {
print("end")
}
}
}
}
}
extension Array {
var json: String? {
do {
let data = try JSONSerialization.data(withJSONObject: self, options: .prettyPrinted)
return String(data: data, encoding: String.Encoding.utf8)
} catch {
return nil
}
}
}
extension Encodable {
var array: [Any]? {
do {
guard let data = try? JSONEncoder().encode(self) else { return nil }
return (try? JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.fragmentsAllowed)).flatMap{$0 as? [Any]}
} catch {
return nil
}
}
var dictionary: [String: Any]? {
do {
guard let data = try? JSONEncoder().encode(self) else { return nil }
return (try? JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.fragmentsAllowed) as? [String : Any])
} catch {
return nil
}
}
}
class MGHelper<T:Codable> {
static func transFormObject<T:Codable>(jsonString: String) -> T? {
if let data = jsonString.data(using: .utf8) {
let objects = try? JSONDecoder().decode(T.self, from: data)
return objects
}else {
return nil
}
}
func getDictionaryFromJSONString(jsonString:String) ->NSDictionary{
let jsonData:Data = jsonString.data(using: .utf8)!
let dict = try? JSONSerialization.jsonObject(with: jsonData, options: .mutableContainers)
if dict != nil {
return dict as! NSDictionary
}
return NSDictionary()
}
func getArrayFromJSONString(jsonString:String) -> Array<Any> {
let jsonData:Data = jsonString.data(using: .utf8)!
let dict = try? JSONSerialization.jsonObject(with: jsonData, options: .mutableContainers)
if dict != nil {
return dict as! Array
}
return Array()
}
}
extension Array {
var json: String? {
do {
let data = try JSONSerialization.data(withJSONObject: self, options: .prettyPrinted)
return String(data: data, encoding: String.Encoding.utf8)
} catch {
return nil
}
}
}
extension Encodable {
var array: [Any]? {
do {
guard let data = try? JSONEncoder().encode(self) else { return nil }
return (try? JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.fragmentsAllowed)).flatMap{$0 as? [Any]}
} catch {
return nil
}
}
var dictionary: [String: Any]? {
do {
guard let data = try? JSONEncoder().encode(self) else { return nil }
return (try? JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.fragmentsAllowed) as? [String : Any])
} catch {
return nil
}
}
}
struct Group : Codable {
let controlGroup : [String]?
let testGroup : [String]?
enum CodingKeys: String, CodingKey {
case controlGroup = "control_group"
case testGroup = "test_group"
}
init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
controlGroup = try values.decodeIfPresent([String].self, forKey: .controlGroup)
testGroup = try values.decodeIfPresent([String].self, forKey: .testGroup)
}
}
struct RootClass : Codable {
let experimentId : String?
let feature : String?
let groups : Group?
enum CodingKeys: String, CodingKey {
case experimentId = "experiment_id"
case feature = "feature"
case groups = "groups"
}
init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
experimentId = try values.decodeIfPresent(String.self, forKey: .experimentId)
feature = try values.decodeIfPresent(String.self, forKey: .feature)
groups = try values.decode(Group.self, forKey: .groups)
}
}