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 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];
}
如何在密钥imdbRating的商业模型对象中保存数据
答案 0 :(得分:0)
您指定的Json不是数组。这是一个NSDictionary
对象。另外,因为这是一个NSDictionary,所以你不需要使用for循环。
第二件事是你的response
变量出错了。
response = [json valueForKey:@"imdbRating"];
User * userObj = [[User alloc]init];
userObj.movieRating = [json valueForKey:@"imdbRating"];
[response addObject:userObj];
此处response
变量不是数组,即使您之前已将其分配为NSMutableArray
。当您在行response
response = [json valueForKey:@"imdbRating"];
中分配新值时
根据NSNumber
的类型,您的变量类型将为NSString
或[json valueForKey:@"imdbRating"]
。
因此请删除行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_Rating
。因为您的方法接受NSMutableArray作为参数。但是,当您为方法提供价值时,NSDictionay [delegate getIMDBMovie_Rating:response];
(您可以NSLog(@"%@", [response class]);
检查)。
因此,在getIMDBMovie_Rating
方法中,您正在访问[retrievedData objectAtIndex:0];
。 objectAtIndex不适用于NSDictionay,这就是你的应用程序崩溃的原因。
答案 1 :(得分:-1)
试试这个,
-(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];
}