playground 简易json字符串转化Model

最近被公司超长的json【惊悚】给烦死了,趁着午休简易写了一个,还有很多改进之处,慢慢完善吧

1 完善出转化Object的

2 完善出转化Swift

3 晚上随意转化swift Object

比较粗糙六个Tagextension String: TMObjcProperty{func convertObjcProperty() -> String { return """ @property(copy,nonatomic) NSString * \(self); """ } func convertObjcClassProperty() -> String{ return """ @property(copy,nonatomic) \(self.capitalized) * \(self); """ } func convertObjcArrayProperty() -> String{ return """ @property(strong,nonatomic) NSArray<\(self.capitalized)*> * \(self); """ } func convertObjcStringProperty() -> String { return """ @property(copy,nonatomic) NSString * \(self); """ } func convertObjcNumberProperty() -> String { return """ @property(assign, nonatomic) NSNumber * \(self); """ }} var pName = TMObjecConvertProperty.init("name").propertyvar item = ["name":"Jack", "list":["name":"Jack"]] as String : Anyextension Dictionary{ func toModel(_ className: String = "ItemClassName", superClassName:String = "NSObject", frameworks:String = "#import<UIKit/UIKit.h>") -> String{ var container:String = String var imports = frameworks for (key, value) in self { let k = "(key)" switch value { case is String : container.append( k.convertObjcProperty()) break case is Double: container.append( k.convertObjcNumberProperty()) case is Int: container.append( k.convertObjcNumberProperty()) case is Bool: container.append( k.convertObjcNumberProperty()) break case is Array<Any> : container.append( k.convertObjcArrayProperty()) print((value as! Array<Any>).toModel(k.capitalized)) break case is Dictionary: container.append( k.convertObjcClassProperty()) imports.append(""" #import"\(k.capitalized).h" """) print((value as! Dictionary<String, Any>).toModel(k.capitalized)) break default: print("") } } return """ \(imports.joined(separator: "\n")) @interface \(className.capitalized):\(superClassName) \(container.joined(separator: "\n")) @end\n @interface \(className.capitalized)() //TODO 子这里补全extension @end @implementation \(className.capitalized) @end """ }} extension Array{ func toModel(_ propertyName: String = "list", className: String = "Item") -> String{ var dest = "" if self.isEmpty{ dest = "@property(nonatomic, strong) NSArray (propertyName);\n" }else{ let value = self.first! switch value { case is Dictionary<String, Any> : dest = "@property(nonatomic, strong) NSArray<(className.capitalized)> *(propertyName);\n" print((value as! Dictionary<String, Any> ).toModel(propertyName.capitalized)) break case is String: dest = "@property(nonatomic, strong) NSArray<String*> *\(propertyName);\n" break case is Bool: dest = "@property(nonatomic, strong) NSArray<NSNumber*> *\(propertyName);\n" break case is Int: dest = "@property(nonatomic, strong) NSArray<NSNumber*> *\(propertyName);\n" break case is Double: dest = "@property(nonatomic, strong) NSArray<NSNumber*> *\(propertyName);\n" break default: dest = "" } } return dest }}//print(["name":"jack", "age":12,"height":175.3, "sub":"name":"Jack", "items":1,2,3].toModel()) //print("\n") //print(["name":"JJJ"].toModel())do{ let jsonObj = try JSONSerialization.jsonObject(with: json.data(using: String.Encoding.utf8)!, options: JSONSerialization.ReadingOptions.allowFragments) as! Dictionary<String, Any> print(jsonObj.toModel("TlbModel"))}catch{ print(error.localizedDescription) }

测试用例print(["name":"jack", "age":12,"height":175.3, "sub":["name":"Jack"], "items":[1,2,3]].toModel()) print("\n") print([["name":"JJJ"]].toModel())

测试结果

#import<UIKit/UIKit.h>
@interface Sub:NSObject
@property(copy,nonatomic) NSString * name;
@end



@interface Sub()
//TODO 子这里补全extension

@end

@implementation Sub

@end
@property(nonatomic, strong) NSArray<NSNumber*> *Items;

#import<UIKit/UIKit.h>
#import"Sub.h"
@interface Itemclassname:NSObject
@property(assign, nonatomic) NSNumber * height;
@property(assign, nonatomic) NSNumber * age;
@property(copy,nonatomic) Sub * sub;
@property(copy,nonatomic) NSString * name;
@property(strong,nonatomic) NSArray<Items*> * items;
@end



@interface Itemclassname()
//TODO 子这里补全extension

@end

@implementation Itemclassname

@end


#import<UIKit/UIKit.h>
@interface List:NSObject
@property(copy,nonatomic) NSString * name;
@end



@interface List()
//TODO 子这里补全extension

@end

@implementation List

@end
@property(nonatomic, strong) NSArray<Item*> *list;

原创声明,本文系作者授权云+社区发表,未经许可,不得转载。

如有侵权,请联系 yunjia_community@tencent.com 删除。

编辑于

我来说两句

0 条评论
登录 后参与评论

扫码关注云+社区

领取腾讯云代金券