JSOn解析数据显示iphone中相同对象的不同值
当我输入NSLog affecteddate值时,它显示正确,但当我将其存储在var中时,它给出错误的值JSOn解析数据显示iphone中相同对象的不同值,iphone,objective-c,Iphone,Objective C,当我输入NSLog affecteddate值时,它显示正确,但当我将其存储在var中时,它给出错误的值 SBJsonParser *parser= [[SBJsonParser alloc] init]; NSString *url = [NSString stringWithFormat:@"http://www.krsconnect.no/community/api.html?method=bareListEventsByCategory&appid=620&cat
SBJsonParser *parser= [[SBJsonParser alloc] init];
NSString *url = [NSString stringWithFormat:@"http://www.krsconnect.no/community/api.html?method=bareListEventsByCategory&appid=620&category-selected=%@&counties-selected=Vest-Agder,Aust-Agder&range=0-10", test];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
NSDictionary *object = [parser objectWithString:json_string error:nil];
NSArray *results = [parser objectWithString:json_string error:nil];
appDelegate.books1 = [[NSMutableArray alloc] init];
appDelegate.dates =[[NSMutableArray alloc]init];
appDelegate.books2=[[NSMutableArray alloc]init];
appDelegate.books3=[[NSMutableArray alloc]init];
appDelegate.books4=[[NSMutableArray alloc]init];
appDelegate.books5=[[NSMutableArray alloc]init];
appDelegate.books6=[[NSMutableArray alloc]init];
appDelegate.books7=[[NSMutableArray alloc]init];
appDelegate.books8=[[NSMutableArray alloc]init];
appDelegate.books9=[[NSMutableArray alloc]init];
appDelegate.books10=[[NSMutableArray alloc]init];
NSArray *activitiesArray;
mycount=[results count];
for (int j=0;j<[results count]; j++) {
NSDictionary *dictOne = [results objectAtIndex:j];
NSLog(@"%@ - %@", [dictOne objectForKey:@"date"]);
Date *aDate = [[Date alloc] initWithDictionary:[results objectAtIndex:j]];
[appDelegate.dates addObject:aDate];
[aDate release];
activitiesArray = [dictOne objectForKey:@"events"];
for (int i=0; i<[activitiesArray count]; i++) {
int testcount =[activitiesArray count];
NSDictionary *dictTwo = [activitiesArray objectAtIndex:i];
NSDictionary *eventDict=[dictTwo objectForKey:@"event"];
// NSLog(@"%@ - %@", [dictOne objectForKey:@"date"]);
// NSLog(@"%@ - %@", [dictTwo objectForKey:@"affectedDate"]);
// NSLog(@"%@ - %@", [eventDict objectForKey:@"location"]);
NSInteger*date=[dictTwo objectForKey:@"affectedDate"];
NSInteger*affecteddate=[dictTwo objectForKey:@"affectedDate"];
NSLog(@"%@ - %@", [dictTwo objectForKey:@"affectedDate"]);
NSString*appId =[eventDict objectForKey:@"appId"];
NSLog(@"%@ - %@", [eventDict objectForKey:@"eventId"]);
NSInteger*eventId=[eventDict objectForKey:@"eventId"];
int next=[affecteddate intValue];
int next_int = [eventId intValue];
NSString*location=[eventDict objectForKey:@"location"];
NSString*municipality=[eventDict objectForKey:@"municipality"];
NSString*title=[eventDict objectForKey:@"title"];
Book1 *aBook=[[Book1 alloc] initWithDate:date affecteddate:affecteddate eventId:eventId location:location municipality:municipality title:title];
首先,你有:
NSInteger*affecteddate=[dictTwo objectForKey:@"affectedDate"];
后来
intValue
对整数无效
您可能想要:
NSNumber *date = [dictTwo objectForKey:@"affectedDate"];
NSNumber *affecteddate = [dictTwo objectForKey:@"affectedDate"];
可以在book对象中声明这样的值来指定此值
NSNumber *affecteddate;
@propert(nonatomic,retain)
这里什么是“正确的”,什么是“错误的”?您认为“正确”的NSLog输出是什么?您认为“错误”的ivar包含什么您不需要一页代码来演示这一点。我从JSON数据中获取这些数据,我已签入数据NSLog out是正确的,我还发布了JSON解析代码,这给出的worng值与我在eventId中使用的值不符,如上所示,它的工作特性我发布了Book类代码,那么我如何在那里声明affecteddate[dictTwo objectForKey:@“affectedDate”]返回一个对象,可能是NSInteger*,而不是int。只有对象才能放入字典、数组或其他对象容器类型中。因此,如何声明此对象并从书本访问它们ObjectInteger不是一个对象,而是一个int。NSNumber是一个可以保存数字的对象。
NSNumber *date = [dictTwo objectForKey:@"affectedDate"];
NSNumber *affecteddate = [dictTwo objectForKey:@"affectedDate"];
NSNumber *affecteddate;
@propert(nonatomic,retain)