2013-04-23 4 views
0

JSON String to obj-c 클래스를 구문 분석하기 위해 JSON Accelerator (http://nerdery.com/json-accelerator)를 사용하고 있습니다.NSArray JSON 값 가져 오기

그러나 JSON NSArray 값을 가져 오는 데 문제가 있습니다.

인 JSON 문자열 값 : I는 (이하 "성분"어레이 내부)은 "component_id"값을 액세스 할

{ 
    "status": { 
     "code": 200, 
     "description": "OK" 
    }, 
    "message": "", 
    "data": { 
     "app_id": 1, 
     "name": "metrodummy", 
     "description": "dummies are dummies", 
     "website": "metrofy.com", 
     "twitter": "twittwat", 
     "facebook": "faceycace", 
     "homepage": { 
      "homepage_id": 1, 
      "background": "bg", 
      "icon": "homepageico", 
      "image_position": "Top", 
      "image_position_param": "value;", 
      "image_position_value": "Top;", 
      "text": "lalilulala", 
      "text_position": "Left Top", 
      "text_position_param": "value;", 
      "text_position_value": "LeftTop;", 
      "text_color": "green", 
      "layout": { 
       "layout_id": 0, 
       "layout_name": null, 
       "layout_thumbnail": null, 
       "layout_code": "codecode", 
       "device_type": "iOS", 
       "layout_template": null 
      } 
     }, 
     "app_design": { 
      "application_design_id": 1, 
      "icon": "ico", 
      "background": "bg", 
      "splash_screen": "splashy", 
      "primary_font": "Courier New", 
      "primary_font_param": "", 
      "primary_font_value": "", 
      "secondary_font": "Verdana", 
      "secondary_font_param": "", 
      "secondary_font_value": "", 
      "layout": { 
       "layout_id": 0, 
       "layout_name": null, 
       "layout_thumbnail": null, 
       "layout_code": "codecode", 
       "device_type": "iOS", 
       "layout_template": null 
      } 
     }, 
     "components": [ 
      { 
       "component_id": 1, 
       "name": "berita nyos", 
       "background": "bg", 
       "icon": "ico", 
       "image_position": "Top", 
       "image_position_param": "value;", 
       "image_position_value": "Top;", 
       "enable_facebook": true, 
       "enable_email": true, 
       "enable_twitter": true, 
       "enable_like": true, 
       "enable_rate": true, 
       "enable_comment": true, 
       "endpoint_api": "api", 
       "layout": { 
        "layout_id": 0, 
        "layout_name": null, 
        "layout_thumbnail": null, 
        "layout_code": "codecode", 
        "device_type": "iOS", 
        "layout_template": null 
       } 
      }, 
      { 
       "component_id": 2, 
       "name": "berita lama", 
       "background": "bg", 
       "icon": "ico", 
       "image_position": "Top", 
       "image_position_param": "value;", 
       "image_position_value": "Top;", 
       "enable_facebook": true, 
       "enable_email": true, 
       "enable_twitter": true, 
       "enable_like": true, 
       "enable_rate": true, 
       "enable_comment": true, 
       "endpoint_api": "api", 
       "layout": { 
        "layout_id": 0, 
        "layout_name": null, 
        "layout_thumbnail": null, 
        "layout_code": "codecode", 
        "device_type": "iOS", 
        "layout_template": null 
       } 
      }, 
      { 
       "component_id": 5, 
       "name": "product component", 
       "background": "bg", 
       "icon": "ico", 
       "image_position": "Top", 
       "image_position_param": "value;", 
       "image_position_value": "Top;", 
       "enable_facebook": true, 
       "enable_email": true, 
       "enable_twitter": true, 
       "enable_like": true, 
       "enable_rate": true, 
       "enable_comment": true, 
       "endpoint_api": "api", 
       "layout": { 
        "layout_id": 0, 
        "layout_name": null, 
        "layout_thumbnail": null, 
        "layout_code": "codecode", 
        "device_type": "iOS", 
        "layout_template": null 
       } 
      } 
     ], 
     "is_current": true, 
     "is_published": false 
    } 
} 

코드 행 이용 :

NSDictionary *jsonContent = [NSJSONSerialization JSONObjectWithData:response options:0 error:&jsonParsingError]; 

MTJsonBaseModel *baseClass = [MTJsonBaseModel modelObjectWithDictionary:jsonContent]; 

NSArray *dataComponents = [NSArray arrayWithArray:baseClass.data.components]; 

[[self.dataComponents objectAtIndex:0] valueForKey:@"component_id"] 

을 그냥 오류 얻을 것 : Btw는

Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<MTComponentsModel 0x7566c20> valueForUndefinedKey:]: this class is not key value coding-compliant for the key component_id.' 

을 MTJsonBaseModel 및 MTComponentsModel은 JS에 의해 생성되는 JSON 클래스입니다 ON Accelerator.

저는 'valueForKey :'를 사용하여 JSON 배열 값에 액세스 할 수 있습니까? 왜 나는 오류가 발생하지만 'valueForKey :'값을 @ "icon"또는 @ "background"로 변경하면 정상적으로 작동합니다.

답변

1
NSDictionary *jsonContent = [NSJSONSerialization JSONObjectWithData:response options:0 error:&jsonParsingError]; 
NSArray *arr = [[jsonContent objectForKey:@"data"] objectForKey:@"components"]; 

// To read array Components 
[arr enumerateObjectsUsingBlock:^(id object, NSUInteger index, BOOL *stop){ 
    NSLog(@"%@",[object valueForKey:@"component_id"]); 
}];