获取jSON导致应用程序崩溃?我应该怎么做才能解决它。我想在userObj.movi​​e评级中存储响应

时间:2014-08-08 04:55:31

标签: 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 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的商业模型对象中保存数据

2 个答案:

答案 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];

}