自定义类的实例化对象转JSON--结合Runtime
来源:互联网 发布:厦门老年大学网络报名 编辑:程序博客网 时间:2024/05/29 15:12
如何将自定义类的实例化对象转换为JSON数据
我所用的方法是通过Runtime的相关方法,先后两步来实现的。具体往下看:
新建一个类命名为《UserClass》
- 展示.h文件UserClass.h
// UserClass.h// MyPickerView// Created by LiynXu on 16/3/7.// Copyright © 2016年 LiynXu. All rights reserved.#import <Foundation/Foundation.h>@interface UserClass : NSObject@property (nonatomic,strong) NSString *name;@property (nonatomic,strong) NSString *password;@property (nonatomic,assign) NSInteger age;@property (nonatomic,strong) NSString *sex;- (instancetype)initWithName:(NSString *)name Password:(NSString *)password;- (NSDictionary *)convertToDictWithObject:(NSObject *)object;//主要使用这个方法得到一个字典@end
由上面的头文件可以看出我所建立的类有4个属性:name,password,age,sex。
并且包含一个初始化方法,和对象转字典的方法(了解过Runtime,所以知道了Runtime可以在运行时某个对象的所有属性,以及属性的相关值)
具体实现方法
- 第一步将自定义类的实例化对象转为字典,这一步需要结合Runtime进行,所以我们需要做一个操作就是导入一个头文件
- #import
// UserClass.m// MyPickerView// Created by LiynXu on 16/3/7.// Copyright © 2016年 LiynXu. All rights reserved.#import "UserClass.h"#import <objc/runtime.h>//当前需要使用使用运行时库@implementation UserClass-(instancetype)initWithName:(NSString *)name Password:(NSString *)password{ self= [super init]; if (self) { _name = name; _password = password; } return self;}
上面的主要是一个初始化方法,重要的是下面,下面,下面!
- (NSDictionary *)convertToDictWithObject:(NSObject *)object{//获取当前对象的所有属性以及属性的值 NSMutableDictionary *Dict = [NSMutableDictionary dictionary]; unsigned int outCount, i; objc_property_t *properties = class_copyPropertyList([object class], &outCount); //获取所有属性以及属性的值,并且转换为一个字典 for (i = 0; i<outCount; i++){ objc_property_t property = properties[i]; const char* char_f =property_getName(property); NSString *propertyName = [NSString stringWithUTF8String:char_f]; id propertyValue = [self valueForKey:(NSString *)propertyName]; if (propertyValue) [Dict setObject:propertyValue forKey:propertyName]; } free(properties); return Dict;}@end
程序入口main.m 包含具体示例代码
//// main.m// JsonCode//// Created by LiynXu on 16/3/7.// Copyright © 2016年 LiynXu. All rights reserved.//#import <Foundation/Foundation.h>#import "UserClass.h"int main(int argc, const char * argv[]) { @autoreleasepool { UserClass *user = [[UserClass alloc] initWithName:@"name" Password:@"password"]; NSDictionary *codeDict = [user convertToDictWithObject:user];//对象转字典, NSLog(@"%@",codeDict); NSData *jsonData = [NSJSONSerialization dataWithJSONObject:codeDict options:NSJSONWritingPrettyPrinted error:nil];//字典转json code NSLog(@"%@",jsonData); NSFileManager *fileManager = [NSFileManager defaultManager]; NSString *path = [NSString stringWithFormat:@"%@/Document/jsonData.json",NSHomeDirectory()]; BOOL res=[fileManager createFileAtPath:path contents:nil attributes:nil]; if (res) { NSLog(@"文件创建成功: %@" ,path); }else NSLog(@"文件创建失败: %@" ,path); NSString *jsonStrong = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; //json转字符串 NSLog(@"%@",jsonStrong); NSDictionary *decodeDict = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil]; //json Decode NSLog(@"%@",decodeDict); } return 0;}
0 0
- 自定义类的实例化对象转JSON--结合Runtime
- python中自定义类对象json字符串化的方法
- SpringMVC和JSon结合实现jsp和服务端对象的相互传递(写的很好,有实例)
- struts2 + ajax + json的结合使用,实例讲解
- struts2 + ajax + json的结合使用,实例讲解
- struts2 + ajax + json的结合使用,实例讲解
- struts2 + ajax + json的结合使用--实例讲解
- struts2 + ajax + json的结合使用,实例讲解
- struts2 + ajax + json的结合使用,实例讲解
- struts2 + ajax + json的结合使用,实例讲解
- struts2 + ajax + json的结合使用,实例讲解
- struts2 + ajax + json的结合使用,实例讲解
- 自定义的Java对象转JSON串的方法
- 如何用runtime对实例对象进行数据化?
- Json 与IList 、DataSet 以及实例类对象之间的互转
- ajax 加载json对象的实例
- json自定义格式化输出的对象
- 模板,函数对象,泛型结合在一起的实例代码
- HDU 1164 Eddy's research I(素因子拆分)
- iOS 项目国际化
- 意内而言外也——《语文常谈》读书笔记(5)
- 错误:unable to execute command: Segmentation fault: 11
- 职场潜规则你能忍受到第几条?
- 自定义类的实例化对象转JSON--结合Runtime
- ActiveMQ+Spring工程创建详解(附工程文件)
- Java异常
- java并发编程实战 ThreadLocal
- DevExpress Winform控件颜色主题机制
- 折腾gcc/g++链接时.o文件及库的顺序问题
- 量化投资策略:常见的几种Python回测框架(库)
- 为什么经常跳槽的程序员工资反倒更高了,而不跳的.......
- Python cartopy 使用地图工具,给出经纬度,画出受影响的中国省份