简体   繁体   中英

JSON Data Not Parsed in iOS

i am Newbie in iOS Development. I want to Parse an JSON Data From My WebServices for that I have written folliwing code

- (void)viewDidLoad
{
 NSURL * url=[NSURL URLWithString:[NSString stringWithFormat:@"http://www.janvajevu.com/webservice/latest_post.php?page=%d",pageNum]];
dispatch_async(kBgQueue, ^{
    data = [NSData dataWithContentsOfURL: url];
    [self performSelectorOnMainThread:@selector(fetchedData:) withObject:data waitUntilDone:YES];
});

}
-(void)fetchedData:(NSData *)responsedata
{
if (responsedata.length > 0)
{
    NSError* error;
    self.json= [NSJSONSerialization JSONObjectWithData:responsedata options:kNilOptions error:&error];
    if ([[_json objectForKey:@"data"] isKindOfClass:[NSArray class]])
    {
        NSArray *arr = (NSArray *)[_json objectForKey:@"data"];
        [self.navuArray addObjectsFromArray:arr];
        [self.navuTable reloadData];
         NSLog(@"JSON Data %@",self.navuArray);
    }
    self.navuTable.hidden=FALSE;
}

Here Self.json is A JSON Dictionary and self.navuArray is my NSMutableArray when i print my Array then It returns Num. Please Give me Solution for it.

Its your navuArray that is not initialized before you are adding objects to it from array. Initialize it before you call for your data and then add objects to it.

Try this code

id jsonObject = [NSJSONSerialization JSONObjectWithData:webData options:kNilOptions error:&error];
  table = [[UITableView alloc]init];

    [dict objectForKey:@"data"];
    NSArray *array = (NSArray *)jsonObject;
    if ([[dict objectForKey:@"data"] isKindOfClass:[NSArray class]])
    {
     for(int i=0;i<[array count];i++)
     {

        dict1 = [array objectAtIndex:i];
        [postIdArr addObject:[dict objectForKey:@"post_id]];
        [postTitleArr addObject:[dict objectForKey:@"post_title"]];
        [postTitleSlungArr addObject:[dict objectForKey:@"post_title_slug"]];
        NSString *dateId = [dict objectForKey:@"post_date"];
        NSArray *dateArray = [dateId componentsSeparatedByString:@" "];
        [dateArrs addObject:[dateArray objectAtIndex:0]];
        [timeArrs addObject:[dateArray objectAtIndex:1]];
        //[dateIdArr addObject:[dict objectForKey:@"date"]];
        [postViewsArr addObject:[dict objectForKey:@"post_views"]];
        [imageArr addObject:[dict objectForKey:@"feature_image"]];
        [shortArr addObject:[dict objectForKey:@"short_description"]];
        [nameArr addObject:[dict objectForKey:@"author_name"]];


    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM