iOS json字段转属性
来源:互联网 发布:java开源电商系统 编辑:程序博客网 时间:2024/04/25 19:23
最近写程序总是遇到这么一个问题: 网络获取数据后,我们需要将一些信息转为类,方便后面的操作。例如:
list 里面的字段,我们需要转成一个类的属性。例如 转为下面的样子:
@interface OffLinePublicModel : NSObject@property (nonatomic, strong) NSString *orderno;@property (nonatomic, strong) NSString *status;@property (nonatomic, strong) NSString *statusflag;@property (nonatomic, strong) NSString *Id;@property (nonatomic, strong) NSString *userid;@property (nonatomic, strong) NSString *companyid;@property (nonatomic, strong) NSString *companyname;@property (nonatomic, strong) NSString *areaid;@property (nonatomic, strong) NSString *areaname;@property (nonatomic, strong) NSString *rootid;@property (nonatomic, strong) NSString *parentids;@property (nonatomic, strong) NSString *channelid;@property (nonatomic, strong) NSString *channelname;@property (nonatomic, strong) NSString *isfinish;@property (nonatomic, strong) NSString *releasetime;@property (nonatomic, strong) NSString *title;@property (nonatomic, strong) NSString *desc;@property (nonatomic, strong) NSString *gongsi;@property (nonatomic, strong) NSString *imageurl;@property (nonatomic, strong) NSString *infotype;;@property (nonatomic, strong) NSString *personname;@property (nonatomic, strong) NSString *personphone;@property (nonatomic, strong) NSArray *imagesList;@property (nonatomic, strong) NSString *year;@property (nonatomic, strong) NSString *salary;@property (nonatomic, strong) NSString *edu;@property (nonatomic, strong) NSString *num;@property (nonatomic, strong) NSString *age;@property (nonatomic, strong) NSString *place;@property (nonatomic, strong) NSString *estate;@property (nonatomic, strong) NSString *numshi;@property (nonatomic, strong) NSString *numting;@property (nonatomic, strong) NSString *numwei;@property (nonatomic, strong) NSString *measure;@property (nonatomic, strong) NSString *sheshi;@property (nonatomic, strong) NSString *numlou;@property (nonatomic, strong) NSString *numall;@property (nonatomic, strong) NSString *ispersonal;@property (nonatomic, strong) NSString *money;@property (nonatomic, strong) NSString *qq;@property (nonatomic, strong) NSString *usage;@property (nonatomic, strong) NSString *color;@property (nonatomic, strong) NSString *mode ;-(instancetype)initWithDictionary:(NSDictionary *)dictionary;@end#import "OffLinePublicModel.h"#import "NSDictionary+StringValue.h"@implementation OffLinePublicModel-(instancetype)initWithDictionary:(NSDictionary *)dictionary{ self = [super init]; if (self) { self.orderno = [dictionary stringValueForKey:@"orderno"]; self.status = [dictionary stringValueForKey:@"status"]; self.statusflag = [dictionary stringValueForKey:@"statusflag"]; self.Id = [dictionary stringValueForKey:@"id"]; self.userid = [dictionary stringValueForKey:@"userid"]; self.companyid = [dictionary stringValueForKey:@"companyid"]; self.companyname = [dictionary stringValueForKey:@"companyname"]; self.areaid = [dictionary stringValueForKey:@"areaid"]; self.areaname = [dictionary stringValueForKey:@"areaname"]; self.rootid = [dictionary stringValueForKey:@"rootid"]; self.parentids = [dictionary stringValueForKey:@"parentids"]; self.channelid = [dictionary stringValueForKey:@"channelid"]; self.channelname = [dictionary stringValueForKey:@"channelname"]; self.isfinish = [dictionary stringValueForKey:@"isfinish"]; self.releasetime = [dictionary stringValueForKey:@"releasetime"]; self.title = [dictionary stringValueForKey:@"title"]; self.desc = [dictionary stringValueForKey:@"desc"]; self.gongsi = [dictionary stringValueForKey:@"gongsi"]; self.imageurl = [dictionary stringValueForKey:@"imageurl"]; self.infotype = [dictionary stringValueForKey:@"infotype"]; self.personname = [dictionary stringValueForKey:@"personname"]; self.personphone = [dictionary stringValueForKey:@"personphone"]; self.imagesList = [dictionary objectForKey:@"imagesList"]; self.year = [dictionary stringValueForKey:@"year"]; self.salary = [dictionary stringValueForKey:@"salary"]; self.edu = [dictionary stringValueForKey:@"edu"]; self.num = [dictionary stringValueForKey:@"num"]; self.age = [dictionary stringValueForKey:@"age"]; self.place = [dictionary stringValueForKey:@"place"]; self.estate = [dictionary stringValueForKey:@"estate"]; self.numshi = [dictionary stringValueForKey:@"numshi"]; self.numting = [dictionary stringValueForKey:@"numting"]; self.numwei = [dictionary stringValueForKey:@"numwei"]; self.measure = [dictionary stringValueForKey:@"measure"]; self.sheshi = [dictionary stringValueForKey:@"sheshi"]; self.numlou = [dictionary stringValueForKey:@"numlou"]; self.numall = [dictionary stringValueForKey:@"numall"]; self.ispersonal = [dictionary stringValueForKey:@"ispersonal"]; self.money = [dictionary stringValueForKey:@"money"]; self.qq = [dictionary stringValueForKey:@"qq"]; self.usage = [dictionary stringValueForKey:@"usage"]; self.color = [dictionary stringValueForKey:@"color"]; self.mode = [dictionary stringValueForKey:@"mode"]; } return self;}@end
那么,问题来了 。这个要一个一个的敲吗?
苦逼的我一直是挨个敲得 。
今天终于忍不了了,我决定写个东西来缓解一下。但是苦逼的Mac 桌面程序不会写,web不会,怎么办? 没办法 。只能用Xcode 写一个Command Line Tool
在日志输出栏来操作 ,委屈求全相当蛋疼啊。
int main(int argc, const char * argv[]) { @autoreleasepool { // insert code here... char s[10000] ; NSMutableArray *StringArray = [NSMutableArray array]; while (gets(s) &&s[0] != '\0') { NSString *string = [[NSString alloc]initWithCString:s encoding:NSUTF8StringEncoding]; [StringArray addObject:string]; } for (NSString *string in StringArray) { NSString *newString = [string stringByReplacingOccurrencesOfString:@"\"" withString:@""]; newString = [newString stringByReplacingOccurrencesOfString:@" " withString:@""]; newString = [newString stringByReplacingOccurrencesOfString:@"\n" withString:@""]; newString = [newString stringByReplacingOccurrencesOfString:@"\t" withString:@""]; NSArray *array = [newString componentsSeparatedByString:@":"]; newString = array[0]; printf("@property (nonatomic, strong) NSString *%s\n",[newString UTF8String]); } printf("\n\n"); for (NSString *string in StringArray) { NSString *newString = [string stringByReplacingOccurrencesOfString:@"\"" withString:@""]; newString = [newString stringByReplacingOccurrencesOfString:@" " withString:@""]; newString = [newString stringByReplacingOccurrencesOfString:@"\n" withString:@""]; newString = [newString stringByReplacingOccurrencesOfString:@"\t" withString:@""]; NSArray *array = [newString componentsSeparatedByString:@":"]; newString = array[0]; printf("self.%s = [[dictionary stringValueForKey:\"%s\"];\n",[newString UTF8String],[newString UTF8String]); } printf("\n\n\n\n"); } return 0;}
复制上面的字段 粘贴到里面 (由于是个小东西)没有错误处理,所以一定要注意哦,只复制要生成属性的字段。
注意粘贴上后 多加两个回车。
输出样例如下:
@property (nonatomic, strong) NSString *orderno@property (nonatomic, strong) NSString *status@property (nonatomic, strong) NSString *statusflag@property (nonatomic, strong) NSString *id@property (nonatomic, strong) NSString *userid@property (nonatomic, strong) NSString *companyid@property (nonatomic, strong) NSString *companyname@property (nonatomic, strong) NSString *areaid@property (nonatomic, strong) NSString *areaname@property (nonatomic, strong) NSString *rootid@property (nonatomic, strong) NSString *parentids@property (nonatomic, strong) NSString *channelid@property (nonatomic, strong) NSString *channelname@property (nonatomic, strong) NSString *isfinish@property (nonatomic, strong) NSString *releasetime@property (nonatomic, strong) NSString *title@property (nonatomic, strong) NSString *desc@property (nonatomic, strong) NSString *gongsi@property (nonatomic, strong) NSString *imageurl@property (nonatomic, strong) NSString *infotype@property (nonatomic, strong) NSString *personname@property (nonatomic, strong) NSString *personphone@property (nonatomic, strong) NSString *imagesList@property (nonatomic, strong) NSString *year@property (nonatomic, strong) NSString *salary@property (nonatomic, strong) NSString *edu@property (nonatomic, strong) NSString *num@property (nonatomic, strong) NSString *age@property (nonatomic, strong) NSString *place@property (nonatomic, strong) NSString *estate@property (nonatomic, strong) NSString *numshi@property (nonatomic, strong) NSString *numting@property (nonatomic, strong) NSString *numwei@property (nonatomic, strong) NSString *measure@property (nonatomic, strong) NSString *sheshi@property (nonatomic, strong) NSString *numlou@property (nonatomic, strong) NSString *numall@property (nonatomic, strong) NSString *ispersonal@property (nonatomic, strong) NSString *money@property (nonatomic, strong) NSString *qq@property (nonatomic, strong) NSString *usage@property (nonatomic, strong) NSString *color@property (nonatomic, strong) NSString *modeself.orderno = [[dictionary stringValueForKey:"orderno"];self.status = [[dictionary stringValueForKey:"status"];self.statusflag = [[dictionary stringValueForKey:"statusflag"];self.id = [[dictionary stringValueForKey:"id"];self.userid = [[dictionary stringValueForKey:"userid"];self.companyid = [[dictionary stringValueForKey:"companyid"];self.companyname = [[dictionary stringValueForKey:"companyname"];self.areaid = [[dictionary stringValueForKey:"areaid"];self.areaname = [[dictionary stringValueForKey:"areaname"];self.rootid = [[dictionary stringValueForKey:"rootid"];self.parentids = [[dictionary stringValueForKey:"parentids"];self.channelid = [[dictionary stringValueForKey:"channelid"];self.channelname = [[dictionary stringValueForKey:"channelname"];self.isfinish = [[dictionary stringValueForKey:"isfinish"];self.releasetime = [[dictionary stringValueForKey:"releasetime"];self.title = [[dictionary stringValueForKey:"title"];self.desc = [[dictionary stringValueForKey:"desc"];self.gongsi = [[dictionary stringValueForKey:"gongsi"];self.imageurl = [[dictionary stringValueForKey:"imageurl"];self.infotype = [[dictionary stringValueForKey:"infotype"];self.personname = [[dictionary stringValueForKey:"personname"];self.personphone = [[dictionary stringValueForKey:"personphone"];self.imagesList = [[dictionary stringValueForKey:"imagesList"];self.year = [[dictionary stringValueForKey:"year"];self.salary = [[dictionary stringValueForKey:"salary"];self.edu = [[dictionary stringValueForKey:"edu"];self.num = [[dictionary stringValueForKey:"num"];self.age = [[dictionary stringValueForKey:"age"];self.place = [[dictionary stringValueForKey:"place"];self.estate = [[dictionary stringValueForKey:"estate"];self.numshi = [[dictionary stringValueForKey:"numshi"];self.numting = [[dictionary stringValueForKey:"numting"];self.numwei = [[dictionary stringValueForKey:"numwei"];self.measure = [[dictionary stringValueForKey:"measure"];self.sheshi = [[dictionary stringValueForKey:"sheshi"];self.numlou = [[dictionary stringValueForKey:"numlou"];self.numall = [[dictionary stringValueForKey:"numall"];self.ispersonal = [[dictionary stringValueForKey:"ispersonal"];self.money = [[dictionary stringValueForKey:"money"];self.qq = [[dictionary stringValueForKey:"qq"];self.usage = [[dictionary stringValueForKey:"usage"];self.color = [[dictionary stringValueForKey:"color"];self.mode = [[dictionary stringValueForKey:"mode"];Program ended with exit code: 0
这里只有NSString 类型的属性,是因为个人觉得 把一些 Int Float 等类型的数据转为NSString更方便以后的操作。 例如 int 类型的 数据是要在Label或其他控件上显示的 ,用的时候还需要转。 当然这个是个人喜好。
上面只是因我个人喜好结果输出的 ,灵活性太低。 可以根据自己的喜好改动一下。
等我学点web方面的知识以后 ,回来完善一下这个功能。
1 0
- iOS json字段转属性
- iOS字典转json 包含字段nsstring
- json数据增加字段属性
- iOS文本字段属性和视图属性
- JS判断json对象字段属性值
- IOS下Category添加属性字段
- iOS下Category添加属性字段
- 解决gson实体转json时当字段值为空时,json串中就不存在该属性
- ALiBABa fastjson去除JSON对象不需要的属性字段
- 基于Jsoncpp对json字符串字段属性值的更改
- ALiBABa fastjson去除JSON对象不需要的属性字段
- 基于JavaScript实现Json数据根据某个字段(json中的某个属性)进行排序
- 字段、属性
- IOS开发—— 获取AppDelegate的属性或字段
- ios 的 字段、方法、属性的访问权限控制
- Jackson 转json 时日期格式化,排除字段,包含字段
- obj 转Json 保留空值字段
- IOS NSDictionary 转json
- 回味圣诞邮件,细品岁末精彩设计
- 多线程 java
- GCM推播訊息(notification)延遲問題
- LeetCode:Unique Paths
- XMLSPY5注册的方法
- iOS json字段转属性
- 【PHP】PHP图像裁剪缩略裁切类源代码及使用方法
- java实现四则运算,难点主要在理解加减乘除优先级以及使用递归
- linux系统奔溃之后的救赎:GRUB
- 15、3SUM
- CCApplicationProtocol学习笔记
- Java基础之知识总结
- 搭建Spring环境
- composer 创建一个github 项目,然后可以通过composer下载