fteching jSON导致应用程序崩溃??我应该怎么做才能解决它。我想将响应存储在userObj.movie\u rating中
json是:fteching jSON导致应用程序崩溃??我应该怎么做才能解决它。我想将响应存储在userObj.movie\u rating中,json,ios7,parse-platform,nsdictionary,Json,Ios7,Parse Platform,Nsdictionary,json是: { "Title":"Kick", "Year":"2009", "Rated":"N/A", "Released":"08 May 2009", "Runtime":"N/A", "Genre":"Comedy, Romance, Thriller", "Director":"Reddy Surender", "Writer":"Abburi Ravi (dialogue), Reddy Surender (screenplay), Vakkantham
{
"Title":"Kick", "Year":"2009", "Rated":"N/A", "Released":"08 May 2009",
"Runtime":"N/A", "Genre":"Comedy, Romance, Thriller", "Director":"Reddy Surender",
"Writer":"Abburi Ravi (dialogue), Reddy Surender (screenplay), Vakkantham Vamsi (story)",
"Actors":"Ravi Teja, Ileana, Shaam, Ali",
"Plot":"A guy with an abnormal lifestyle tries to find thrill and pleasure in every work he does and finally becomes a thief.",
"Language":"Telugu", "Country":"India", "Awards":"1 nomination.",
"Poster":"N/A","Metascore":"N/A",
"imdbRating":"7.6",
"imdbVotes":"736",
"imdvid":"tt1579592",
"Type":"movie",
"Response":"True"
}
代码是:
-(void)parseData:(NSData *)data
{
NSString *responseString = [[NSString alloc] initWithData:data
encoding:NSUTF8StringEncoding];
NSLog(@"responseString %@",responseString);
NSError *error;
NSArray *json = [NSJSONSerialization JSONObjectWithData:data
options:(kNilOptions)
error:&error];
NSLog(@"json data == %@", json);
NSMutableArray *response =[[NSMutableArray alloc]init];
for (int i = 0; i < json.count; i++)
{
response = [json valueForKey:@"imdbRating"];
User * userObj = [[User alloc]init];
userObj.movieRating = [json valueForKey:@"imdbRating"];
[response addObject:userObj];
}
[delegate getIMDBMovie_Rating:response];
}
-(void)parseData:(NSData*)数据
{
NSString*responseString=[[NSString alloc]initWithData:data
编码:NSUTF8StringEncoding];
NSLog(@“responseString%@”,responseString);
n错误*错误;
NSArray*json=[NSJSONSerialization JSONObjectWithData:data
选项:(针织品)
错误:&错误];
NSLog(@“json数据==%@”,json);
NSMutableArray*响应=[[NSMutableArray alloc]init];
for(int i=0;i
如何在业务模型对象中为关键字imdbRating保存数据指定的Json不是数组。这是一个
NSDictionary
对象。此外,由于这是一个NSDictionary,您不需要将其用于循环
第二件事是您的响应
变量出错
你做到了:
response = [json valueForKey:@"imdbRating"];
User * userObj = [[User alloc]init];
userObj.movieRating = [json valueForKey:@"imdbRating"];
[response addObject:userObj];
此处response
变量不是数组,即使您在前面将其分配为NSMutableArray
。当您在response
行中分配新值时:response=[json valueForKey:@“imdbRating”]代码>
根据[json valueForKey:@“imdbRating”]
的类型,您的变量类型将是NSNumber
或NSString
因此,删除行response=[json valueForKey:@“imdbRating”]代码>和它将工作
通过这样做,您将创建一个包含imdbRatings的User
对象数组
更新
根据您的评论,您的代码有:
-(void)getIMDBMovie_Rating:(NSMutableArray*)retrievedData {
if (retrievedData.count > 0)
{
userObj = [retrievedData objectAtIndex:0];
NSLog(@"%@is the value",retrievedData);
iMDB_MovieRatingLabel.text=userObj.movieRating;
}
}
问题出在您的getIMDBMovie\u评级中
。因为您的方法接受NSMutableArray作为参数。但当您为您的方法提供值时,它是NSDictionay[委托getIMDBMovie_Rating:response]代码>(您可以通过NSLog(@“%@,[response class]);
进行检查)
因此,在您的getIMDBMovie_Rating
方法中,您正在访问[retrieveddataobjectatindex:0]代码>。objectAtIndex不适用于N词典,这就是为什么你的应用程序会崩溃。像这样尝试
-(void)parseData:(NSData *)data
{
NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"responseString %@",responseString);
NSError *error;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:(kNilOptions) error:&error];
NSLog(@"json data == %@", json);
NSMutableArray *response =[[NSMutableArray alloc]init];
NSArray *aArray = [json valueForKey:@"imbdRating"];
for (NSDictionary *dictionary in aArray)
{
/* Create a initWithData method in your User Model class and pass the dictionary object to it like,
- (id) initWithData:(NSDictionary *) jsonDictionary
{
self=[super init];
if(self)
{
self.movieRating =[json valueForKey:@"imdbRating"];
} */
User * userObj = [[User alloc]initWithData:dictionary];
[response addObject:userObj];
}
[delegate getIMDBMovie_Rating:response];
}
我是这样做的:-(void)parseData:(NSData*)data{NSString*responseString=[[NSString alloc]initWithData:data encoding:nsertf8stringencoding];NSError*error;NSArray*json=[nsjsjson序列化JSONObjectWithData:data选项:(kNilOptions)error:&error];NSLog(@“json data==%”,json);User*userObj=[[User alloc]init];userObj.movieRating=[json valueForKey:@“imdbRating”];[delegate getIMDBMovie_Rating:json];}但应用程序仍然崩溃[u NSCFDictionary objectAtIndex:]并导致错误。请发送正确的代码来解决它。请先将json变量设置为NSDictionary类型。您可以编辑您的问题并添加方法定义吗?据我所知,问题出在getIMDBMovie\u Rating:
method中。这是一个自定义委托,用于在uiLabel上显示respose vlue为:-(void)getIMDBMovie\u Rating:(NSMutableArray*)retrievedData{if(retrievedData.count>0){userObj=[retrievedData objectAtIndex:0];NSLog(@“%@是值”,retrievedData);iMDB_MovieRatingLabel.text=userObj.movieRating;}}}无kapil getIMDBMovie_评级方法中没有问题,调试器显示URL被正确命中,并且响应未被正确管理以保存在nsdictionaryWhat's error中?哪一行?您的解决方案不太正确:[json valueForKey:@“imbdRating”]
返回一个NSString
,而不是NSMutableArray
。特别是,使用JSONObjectWithData
中提供的选项,所有JSON元素都是不可变的。此外,在这种情况下,使用valueForKey:
检索对象并不可取。