将 Json 数据转换为 iOS 中的对象时出错

Getting error while converting Json data to Object in iOS

我正在使用 API 将 Json 数据转换为对象数据, 请访问我正在使用的 API Please Visit the API page which I am using

这是我的代码的快照,其中突出显示了问题 Issues

这是原始代码

 -(void) retrieveData{
    NSURL * url = [NSURLURLWithString:getDataUrl];
NSData * data = [NSData dataWithContentsOfURL:url];
jsonArray = [NSJSONSerialization JSONObjectWithData:data options: kNilOptions error:nil];

NSLog(@"JsonArray %@", jsonArray);

//setup yougaArray
yougaArray = [[NSMutableArray alloc] init];

//Loop through our jsonArray

for (int i = 0; i<jsonArray.count; i++)
{
    NSString * yId = [[[[jsonArray objectAtIndex:i]objectForKey:@"data"]objectAtIndex:@"categories"]objectForKey:@"id"];
   // NSString * yId = [[jsonArray objectAtIndex:i]objectForKey:@"id"];
    NSString * yName = [[[[jsonArray objectAtIndex:i]objectForKey:@"data"]objectAtIndex:@"categories"]objectForKey:@"name"];
    NSString * yDescription = [[[[jsonArray objectAtIndex:i]objectForKey:@"data"]objectAtIndex:@"categories"]objectForKey:@"description"];
    NSString * yImage = [[[[jsonArray objectAtIndex:i]objectForKey:@"data"]objectAtIndex:@"categories"]objectForKey:@"image"];

    //Add the city object to our citiesArray

    [yougaArray addObject:[[Youga alloc]initWithYougaId:yId andYougaName:yName andYougaDescpription:yDescription andYougaImage:yImage]];

     }
[self.tableView reloadData];

     }

根据您的 JSON 响应,它是一个 Dictionary 类型的对象而不是 array 所以您的代码应该是这样的,

    NSMutableDictionary *dictData = [NSJSONSerialization JSONObjectWithData:data options: kNilOptions error:nil];

    NSLog(@"JsonArray %@", dictData);

       NSArray *jsonArray=[[dictData objectForKey:@"data"] objectForKey:@"categories"];
    //setup yougaArray
    yougaArray = [[NSMutableArray alloc] init];


    for (int i = 0; i<jsonArray.count; i++)
    {


        NSString * yId = [[jsonArray objectAtIndex:i] objectForKey:@"id"];
        // NSString * yId = [[jsonArray objectAtIndex:i]objectForKey:@"id"];
        NSString * yName = [[jsonArray objectAtIndex:i] objectForKey:@"name"];
        NSString * yDescription = [[jsonArray objectAtIndex:i] objectForKey:@"description"];
        NSString * yImage = [[jsonArray objectAtIndex:i] objectForKey:@"image"];

        //Add the city object to our citiesArray

        [yougaArray addObject:[[Youga alloc]initWithYougaId:yId andYougaName:yName andYougaDescpription:yDescription andYougaImage:yImage]];

    }
    [self.tableView reloadData];

希望对你有用。让我知道!! 快乐的编码。 :)

这是JSON:

{
    "meta": {
        "status": "200",
        "msg": "OK"
    },
    "data": {
        "total_pages": 0,
        "total_categories": 2,
        "current_page": 1,
        "next_page": 0,
        "categories": [{
            "id": "2",
            "name": "Articles",
            "description": "Yoga Articles",
            "image": "http:\/\/yoga.lifehealthinfo.com\/uploads\/images\/50_50\/86289272image86289272.jpg"
        }, {
            "id": "1",
            "name": "Poses",
            "description": "Yoga Poses",
            "image": "http:\/\/yoga.lifehealthinfo.com\/uploads\/images\/50_50\/86289272image86289272.jpg"
        }]
    }
}

您的 JSON 是顶级的 NSDictionary!不是NSArray! 此外,避免在同一个 line/instruction 中执行所有 objectForKey:/objectAtIndex:,这样更难阅读,也更难调试,尤其是当您不知道自己在做什么时。 还有,有错误参数的时候就用,不要放nil.

所以:

NSError *errorJSON = nil;
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&errorJSON];
if (errorJSON)
{
    NSLog(@"ErrorJSON: %@", errorJSON);
    return;
}
NSDictionary *dataJSON = [jsonDict objectForKey:@"data"];
NSArray *allCategoriesJSON = [dataJSON objectForKey:@"categories"];
for (NSUIInteger i = 0; i < allCategoriesJSON.count; i ++)
{
    NSDictionary *aCategoryJSON = [allCategoriesJSON objectAtIndex:i];
    NSString yID = [aCategoryJSON objectForKey:@"id"];
    NSString yName = [aCategoryJSON objectForKey:@"name"];
    NSString yDescription = [aCategoryJSON objectForKey:@"description"];
    NSString yImage = [aCategoryJSON objectForKey:@"image"];
    [yougaArray addObject:[[Youga alloc] initWithYougaId:yId andYougaName:yName andYougaDescpription:yDescription andYougaImage:yImage]];
}

你JSON看起来是这样的:

{
"meta": {
    "status": "200",
    "msg": "OK"
},
"data": {
    "total_pages": 0,
    "total_categories": 2,
    "current_page": 1,
    "next_page": 0,
    "categories": [{
        "id": "2",
        "name": "Articles",
        "description": "Yoga Articles",
        "image": "http:\/\/yoga.lifehealthinfo.com\/uploads\/images\/50_50\/86289272image86289272.jpg"
    }, {
        "id": "1",
        "name": "Poses",
        "description": "Yoga Poses",
        "image": "http:\/\/yoga.lifehealthinfo.com\/uploads\/images\/50_50\/86289272image86289272.jpg"
    }]
}
}

现在将您现有的代码替换为以下代码:

NSData * data = [NSData dataWithContentsOfURL:url];
jsonArray = [NSJSONSerialization JSONObjectWithData:data options: kNilOptions error:nil];

NSLog(@"JsonArray %@", jsonArray);

//setup yougaArray
yougaArray = [[NSMutableArray alloc] init];

//Loop through our jsonArray

NSArray *dataArray = [[jsonArray objectForKey@"data"] objectForKey:@"categories"];

for (int i = 0; i < dataArray.count; i++) {
    NSString * yId = [[dataArray objectAtIndex:i] objectForKey:@"id"];
    NSString * yName = [[dataArray objectAtIndex:i] objectForKey:@"name"];
    NSString * yDescription = [[dataArray objectAtIndex:i] objectForKey:@"description"];
    NSString * yImage = [[dataArray objectAtIndex:i] objectForKey:@"image"];

    //Add the city object to our citiesArray

    [yougaArray addObject:[[Youga alloc]initWithYougaId:yId andYougaName:yName andYougaDescpription:yDescription andYougaImage:yImage]];
}

[self.tableView reloadData];

让我知道该解决方案是否适合您,如果有任何问题也请告诉我。

谢谢@Lame,我遵循了你的代码,因为它给了我 6 到 8 个错误,但我理解你的代码和配置错误,因为现在这里是根据我的要求工作的完整解决方案或(完美的答案根据我提出的问题)

-(void) retrieveData{
    NSURL * url = [NSURL URLWithString:getDataUrl];
    NSData * data = [NSData dataWithContentsOfURL:url];
    NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];

    NSDictionary *dataJSON = [jsonDict objectForKey:@"data"];
    NSArray *allCategoriesJSON = [dataJSON objectForKey:@"categories"];

    yougaArray = [[NSMutableArray alloc] init];

    for (int i = 0; i < allCategoriesJSON.count; i ++)
    {
        NSDictionary *aCategoryJSON = [allCategoriesJSON objectAtIndex:i];
        NSString *yId = [aCategoryJSON objectForKey:@"id"];
        NSString *yName = [aCategoryJSON objectForKey:@"name"];
        NSString *yDescription = [aCategoryJSON objectForKey:@"description"];
        NSString *yImage = [aCategoryJSON objectForKey:@"image"];
        [yougaArray addObject:[[Youga alloc] initWithYougaId:yId andYougaName:yName andYougaDescpription:yDescription andYougaImage:yImage]];
    }
    [self.tableView reloadData];