-3

我有 2 个问题:我需要从下面的 JSON 中获取“元素”和“类别”到我的核心数据持久存储/数据库中,我尝试了以下(下面的代码),但我对是否我感到困惑和困惑做对了吗?

当我尝试使用嵌套的 objectForKey 获取元素数组时,我也会遇到错误,为什么以及如何修复它?

错误,我不明白,因为 Elements 是一个数组?

   reason: '-[__NSCFArray objectForKey:]: unrecognized selector sent to instance 0x1cd2f4a0'

我收到带有 AFNetworking 的 JSON,如下所示:

         [[MyAPIClient sharedClient] getPath:domain parameters:nil success:^(AFHTTPRequestOperation *operation, id JSON) {

//IS THIS THE RIGHT WAY TO PROCESS NESTED JSON DATA IN CORE DATA? 

            NSError *error;
            NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:JSON options:0 error:&error];

            NSArray *arrayOfCategoriDictionaries = (NSArray *)[[jsonDict objectForKey:@"Manifacture"] objectForKey:@"Categori"];


        //Categori array to Core data
            for( NSDictionary *d  in arrayOfCategoriDictionaries) {
               Categori *cat = [NSEntityDescription insertNewObjectForEntityForName:@"Categori" inManagedObjectContext:_managedObjectContext];


            }

    //Elements to core data, get an error with objectForKey:@"Elements" ?         

            NSArray *arrayOfElementsDictionaries = [[[jsonDict objectForKey:@"Manifacture"] objectForKey:@"Categori"] objectForKey:@"Elements"];



            for(NSDictionary *d1 in arrayOfRetDictionaries) {
                Elements *elements = [NSEntityDescription insertNewObjectForEntityForName:@"Elements" inManagedObjectContext:_managedObjectContext];

            }






        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {


            if (![_managedObjectContext save:&error]) {
                NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
            }





        }];

嵌套的 json 数据:

   {
  "Manifacture": {
    "Categori": [
      {
        "Elements": [
          {
            "Id": 1,
            "Name": "Door",
            "Description": "Black door with window",
            "Price": 149,
            "CategoriId": 1
          }
        ],
        "Id": 1,
        "Name": "Forret",
        "ElementsId": 1,
        "Manifacture_Id": 1
      }
    ],
    "CarSet": [],
    "Id": 1,
    "Name": "Hummer",
    "Description": "A big car"
  },
  "Id": 2,
  "Name": "Hummer Car Factory",
  "Contactperson": "Adil Bujas",
  "Location": "California",
  "Info": "Hummer LTD",
  "SearchThumbnail": "none",
  "CarPicture": "none",
  "Doors": 5,
  "webpages_MembershipUserId": 4,
  "Manifacture_Id": 1
}

提前感谢您的帮助。

4

3 回答 3

1

我不确定NSJSONSerialization. 我已经看到某些 JSON 类返回只有1 个对象的数组,作为该对象本身。

它应该让你更容易,但它通常会让你更难,因为你期望一个带有 1 个对象的 NSArray,但你得到的是数组第一个对象的 NSDictionary。

如果你这样做会发生什么:

NSLog(@"%@", [[[jsonDict objectForKey:@"Manifacture"] objectForKey:@"Categori"] objectForKey:@"Elements"] class]);

编辑:

啊,如果你仔细看看你的 JSON,你会注意到键Categori也包含一个 NSArray,但你把它当作它包含一个字典。那可能就是您发生错误的地方。

于 2013-02-08T18:01:17.693 回答
0

尝试删除演员并拆分语句以隔离任何问题:

NSDictionary *manifactureDict = [jsonDict objectForKey:@"Manifacture"];
NSArray *arrayOfCategoriDictionaries = [manifactureDict objectForKey:@"Categori"];

或者尝试将内部对象转换为字典:

NSArray *arrayOfCategoriDictionaries = [(NSDictionary*)[jsonDict objectForKey:@"Manifacture"] objectForKey:@"Categori"];
于 2013-02-08T20:47:18.453 回答
0

自己找到了解决方案。

NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:JSON options:0 error:&error];

        NSDictionary *theDict = [jsonDict objectForKey:@"Manifacture"];

            NSArray *categoriArray = [theDict objectForKey:@"Categori"];

            for (NSDictionary *aDictionary in categoriArray) {
            NSDictionary *arrayOfElementsDictionaries = [aDictionary objectForKey:@"Elements"];

    }

无论如何,谢谢你的帮助。

于 2013-02-10T10:43:37.277 回答