在iOS中将Json数据转换为Object时出错

时间:2016-08-02 11:20:58

标签: ios objective-c json nsmutablearray nsmutablestring

我正在使用API​​将Json数据转换为Object数据, 请访问我正在使用的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];

     }

4 个答案:

答案 0 :(得分:2)

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

-(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];

答案 1 :(得分:1)

根据您的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];

希望它适合你。让我知道!! 快乐的编码。 :)

答案 2 :(得分:1)

You 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];

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

答案 3 :(得分:0)

这是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! 此外,避免在同一行/指令中执行所有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]];
}