2014-11-25 108 views
-2

在我的iOS应用中,我正在获取一些通知数据。此数据格式如下:如何将JSON数据格式化为Objective-C变量?

{ 
"data": [ 
    { 
     "to": "1", 
     "from": "0", 
     "text": "Person X liked photo Y.", 
     "read": "1", 
     "date": "1 month ago" 
    }, 
    { 
     "to": "1", 
     "from": "0", 
     "text": "Person X liked status Y.", 
     "read": "1", 
     "date": "1 month ago" 
    }, 
    { 
     "to": "1", 
     "from": "0", 
     "text": "Person X wants to be your friend.", 
     "read": "1", 
     "date": "1 month ago" 
    }, 
    { 
     "to": "1", 
     "from": "0", 
     "text": "Person X is a bit of a stalker.", 
     "read": "1", 
     "date": "1 month ago" 
    }, 
] 
} 

我知道如何与在data元素只有一个变量JSON格式数据,但是这一次得到了与内容的数组。我需要在下面的代码中更改什么?

NSLog(@"connectionDidFinishLoading"); 
NSLog(@"Succeeded! Received %d bytes of data",[self.notificationData length]); 

// convert to JSON 
NSError *notificationError = nil; 
NSDictionary *notificationRes = [NSJSONSerialization JSONObjectWithData:self.notificationData options:NSJSONReadingMutableLeaves error:&myError]; 

NSLog(@"%@", notificationRes); 

// extract specific value... 
NSDictionary *notificationSwitchValues = [notificationRes objectForKey:@"data"]; 

NSLog(@"%@", notificationSwitchValues); 

NSString *text = [notificationSwitchValues objectForKey:@"text"]; 

NSLog(@"%@", text); 
+0

您的代码被打破因为它假设所有的VA字典中的字符是字符串。然而,正如你所说的那样,'data'值是一个数组,所以即使数组中只有一个元素,代码也不会工作。后来它假设'data'是一个字典。这是怎么回事? – trojanfoe 2014-11-25 08:56:24

+0

这是我通常用来将JSON解析为可用数据的代码。但是,是的,通常它会提取字符串。那么我怎么能改变上面的代码来读取所有的'text'变量? – user4191537 2014-11-25 08:58:56

+0

所以问题是如何迭代一个'NSArray'。搜索“objective-c快速枚举”。 – trojanfoe 2014-11-25 09:00:28

回答

0

[notificationRes objectForKey:@"data"]是数组,因此必须

NSArray *notificationSwitchValues = notificationRes[@"data"]; 
NSLog(@"%@", notificationSwitchValues); 
for (NSDictionary *notificationSwitchValue in notificationSwitchValues) { 
    NSString *text = notificationSwitchValue[@"text"]; 
} 
+0

谢谢!这没有窍门:) – user4191537 2014-11-25 09:55:02

0
NSDictionary *notificationSwitchValues = [notificationRes objectForKey:@"data"]; 

上述代码应更换到

NSArray *notificationSwitchValues = [notificationRes objectForKey:@"data"]; 

然后从阵列存取元件,这将是再次从最后一个字典访问dictionary.Then元素。

0
NSMutableArray *entitys = [NSMutableArray new]; 
for (NSDictionary *dictionary in notificationRes) { 
    StrangeJSONData *strangeJSONData = [StrangeJSONData strangeJSONDataFromDictionary:dictionary]; 
    [entitys addObject:strangeJSONData]; 
} 

@interface StrangeJSONData : NSObject 
@property (nonatomic, strong) NSNumber *to; 
@property (nonatomic, strong) NSNumber *from; 
@property (nonatomic, strong) NSNumber *read; 
@property (nonatomic, strong) NSString *date; 
@property (nonatomic, strong) NSString *text; 
+ (instancetype)strangeJSONDataFromDictionary:(NSDictionary *)dictionary; 
@end 

@implementation StrangeJSONData 

+ (instancetype)strangeJSONDataFromDictionary:(NSDictionary *)dictionary { 
    StrangeJSONData *strangeJSONData = [[StrangeJSONData alloc] init]; 
    strangeJSONData.to = dictionary[@"to"]; 
    strangeJSONData.from = dictionary[@"from"]; 
    strangeJSONData.read = dictionary[@"read"]; 
    strangeJSONData.date = dictionary[@"date"]; 
    strangeJSONData.date = dictionary[@"text"]; 
    return strangeJSONData; 
} 

@end