下午好,
我试图存储来自JSON输出的响应,因为我想在" ProfileViewController"中显示。用户的统计数据以及我尝试使用以下功能来存储信息。
目前输出正常,因为数据可以取决于用户,但现在我必须将每个统计数据存储在"变量"对于每个统计数据(我有3个),但是当我运行代码时,它没有显示我的星标,追随者和图片的NSLog ......
你可以帮帮我吗?响应很好,现在我只想将每个统计数据存储在一个变量中。我怎样才能做到这一点?我的代码有什么问题?ProfileViewController - >的 fetchJson :
-(void)fetchJson {
NSString *usersPassword = [SSKeychain passwordForService:@"login" account:@"account"];
NSString *post =[[NSString alloc] initWithFormat:@"usersPassword=%@",usersPassword];
NSLog(@"PostData: %@",post);
NSURL *url=[NSURL URLWithString:@"http://website.com/profile.php"];
NSData * data = [NSData dataWithContentsOfURL:url];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]];
NSError *error = [[NSError alloc] init];
NSHTTPURLResponse *response = nil;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSLog(@"Response code: %ld", (long)[response statusCode]);
if ([response statusCode] >=200 && [response statusCode] <300)
{
NSString *responseData = [[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(@"Response ==> %@", responseData);
SBJsonParser *jsonParser = [SBJsonParser new];
NSDictionary *jsonData = (NSDictionary *) [jsonParser objectWithString:responseData error:nil];
NSInteger success = [(NSNumber *) [jsonData objectForKey:@"success"] integerValue];
NSLog(@"%ld",(long)success);
if(success == 1)
{
NSLog(@"Login SUCCESS");
[_jsonArray removeAllObjects];
_jsonArray = [NSJSONSerialization
JSONObjectWithData:data
options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves
error:&error];
for(int i=0;i<_jsonArray.count;i++)
{
NSDictionary * jsonObject = [_jsonArray objectAtIndex:i];
NSString* stars = [jsonObject objectForKey:@"stars"];
NSLog(@"Stars ==> %@", stars);
NSDictionary * jsonObject2 = [_jsonArray objectAtIndex:i];
NSString* followers = [jsonObject2 objectForKey:@"followers"];
NSLog(@"Followers ==> %@", followers);
NSDictionary * jsonObject3 = [_jsonArray objectAtIndex:i];
NSString* photos = [jsonObject3 objectForKey:@"photos"];
NSLog(@"Pictures ==> %@", photos);
}
}
}
}
JSON输出:
{"success":1,"stars":50,"photos":50,"followers":50}
提前致谢。
答案 0 :(得分:0)
_jsonArray = [NSJSONSerialization JSONObjectWithData:myNSData options:kNilOptions error:&error];
NSDictionary * jsonObject = (NSDictionary *)_jsonArray;
然后检查字典对象。
答案 1 :(得分:0)
jsonData已包含已解析的响应,并且您已成功从中检索成功值,如下所示
NSInteger success = [(NSNumber *) [jsonData objectForKey:@"success"] integerValue];
NSLog(@"%ld",(long)success);
您可以按如下方式检索其他值
[[jsonData objectForKey:@"stars"] integerValue]
等等。
您没有必要使用以下方法
创建基础对象NSJSONSerialization
JSONObjectWithData:data
options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves
error:&error];
并且不需要以下代码块,
for(int i=0;i<_jsonArray.count;i++)
{
NSDictionary * jsonObject = [_jsonArray objectAtIndex:i];
NSString* stars = [jsonObject objectForKey:@"stars"]
NSLog(@"Stars ==> %@", stars);
NSDictionary * jsonObject2 = [_jsonArray objectAtIndex:i];
NSString* followers = [jsonObject2 objectForKey:@"followers"];
NSLog(@"Followers ==> %@", followers);
NSDictionary * jsonObject3 = [_jsonArray objectAtIndex:i];
NSString* photos = [jsonObject3 objectForKey:@"photos"];
NSLog(@"Pictures ==> %@", photos);
}