Ios 如何将JSON数据格式化为objective-c变量?
在我的iOS应用程序中,我正在获取一些通知数据。此数据的格式如下所示:Ios 如何将JSON数据格式化为objective-c变量?,ios,objective-c,json,xcode,Ios,Objective C,Json,Xcode,在我的iOS应用程序中,我正在获取一些通知数据。此数据的格式如下所示: { "data": [ { "to": "1", "from": "0", "text": "Person X liked photo Y.", "read": "1", "date": "1 month ago" }, { "to": "1", "from": "0", "
{
"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);
应将上述代码替换为
NSArray *notificationSwitchValues = [notificationRes objectForKey:@"data"];
然后从数组中访问元素,它将再次成为一个字典。然后从上一个字典中访问元素。我强烈建议您使用一些第三方开源库,这些库可以为您提供这些功能,并提供许多其他功能
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
试试以下方法之一:
- 地幔:
- JSONModel:
[notificationRes objectForKey:@“data”]
是数组,因此需要
NSArray *notificationSwitchValues = notificationRes[@"data"];
NSLog(@"%@", notificationSwitchValues);
for (NSDictionary *notificationSwitchValue in notificationSwitchValues) {
NSString *text = notificationSwitchValue[@"text"];
}
您的代码被破坏,因为它假定字典中的所有值都是字符串。但是,正如您自己所说,
数据
值是一个数组,因此即使数组中只有一个元素,代码也无法工作。然后,它假设数据
是一个字典。发生了什么事?这是我通常用来将JSON解析为可用数据的代码。但是,是的,通常它会获取字符串。那么我如何更改上面的代码来读取所有文本
变量呢?问题是如何迭代NSArray
。搜索“objective-c快速枚举”。您可以使用直接将“NSArray”或字典转换为对象模型
NSArray *notificationSwitchValues = notificationRes[@"data"];
NSLog(@"%@", notificationSwitchValues);
for (NSDictionary *notificationSwitchValue in notificationSwitchValues) {
NSString *text = notificationSwitchValue[@"text"];
}