111*_*110 11 json objective-c nsdictionary ios nsjsonserialization
我有一个json对象:
@interface Order : NSObject
@property (nonatomic, retain) NSString *OrderId;
@property (nonatomic, retain) NSString *Title;
@property (nonatomic, retain) NSString *Weight;
- (NSMutableDictionary *)toNSDictionary;
...
- (NSMutableDictionary *)toNSDictionary
{
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
[dictionary setValue:self.OrderId forKey:@"OrderId"];
[dictionary setValue:self.Title forKey:@"Title"];
[dictionary setValue:self.Weight forKey:@"Weight"];
return dictionary;
}
Run Code Online (Sandbox Code Playgroud)
在字符串中这是:
{
"Title" : "test",
"Weight" : "32",
"OrderId" : "55"
}
Run Code Online (Sandbox Code Playgroud)
我用代码获取字符串JSON:
NSMutableDictionary* str = [o toNSDictionary];
NSError *writeError = nil;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:str options:NSJSONWritingPrettyPrinted error:&writeError];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
Run Code Online (Sandbox Code Playgroud)
现在我需要从JSON字符串创建和映射对象:
NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSError *e;
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:jsonData options:nil error:&e];
Run Code Online (Sandbox Code Playgroud)
这将返回我填写的NSDictionary.我该怎么做才能从这本字典中获取对象?
And*_*eev 22
添加新initWithDictionary:
方法Order
:
- (instancetype)initWithDictionary:(NSDictionary*)dictionary {
if (self = [super init]) {
self.OrderId = dictionary[@"OrderId"];
self.Title = dictionary[@"Title"];
self.Weight = dictionary[@"Weight"];
}
return self;
}
Run Code Online (Sandbox Code Playgroud)
不要忘记将文件添加initWithDictionary
到Order.h
文件中
在获得JSON的方法中:
NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSError *e;
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:jsonData options:nil error:&e];
Order *order = [[Order alloc] initWithDictionary:dict];
Run Code Online (Sandbox Code Playgroud)
Gia*_*one 11
如果对象上的属性名称与JSON字符串中的键匹配,则可以执行以下操作:
要将JSON字符串映射到Object,您需要先将字符串转换为NSDictionary,然后在NSObject上使用一个使用键值编码来设置每个属性的方法.
NSError *error = nil;
NSData *jsonData = ...; // e.g. [myJSONString dataUsingEncoding:NSUTF8Encoding];
NSDictionary *jsonDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingOptionsAllowFragments error:&error];
MyObject *object = [[MyObject alloc] init];
[object setValuesForKeysWithDictionary:jsonDictionary];
Run Code Online (Sandbox Code Playgroud)
如果键不匹配,则可以-[NSObject valueForUndefinedKey:]
在对象类中覆盖NSObject的实例方法.
要将Object映射到JSON,您可以使用Objective-C运行时自动执行此操作.以下适用于任何NSObject子类:
#import <objc/runtime.h>
- (NSDictionary *)dictionaryValue
{
NSMutableArray *propertyKeys = [NSMutableArray array];
Class currentClass = self.class;
while ([currentClass superclass]) { // avoid printing NSObject's attributes
unsigned int outCount, i;
objc_property_t *properties = class_copyPropertyList(currentClass, &outCount);
for (i = 0; i < outCount; i++) {
objc_property_t property = properties[i];
const char *propName = property_getName(property);
if (propName) {
NSString *propertyName = [NSString stringWithUTF8String:propName];
[propertyKeys addObject:propertyName];
}
}
free(properties);
currentClass = [currentClass superclass];
}
return [self dictionaryWithValuesForKeys:propertyKeys];
}
Run Code Online (Sandbox Code Playgroud)
假设您的属性名称和字典键相同,您可以使用此函数转换任何对象
- (void) setObject:(id) object ValuesFromDictionary:(NSDictionary *) dictionary
{
for (NSString *fieldName in dictionary) {
[object setValue:[dictionary objectForKey:fieldName] forKey:fieldName];
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
19205 次 |
最近记录: |