2012-12-16 2 views
3

영어 죄송합니다 :(.... 내가 restkit 간단한 JSON을 코딩 할 알고있다. 그러나 매우 혼란 JSON은 다음과 같다. 'openingHour를 들어어떻게 얻기에 관계 키

{ 
    "success": { 
     "code": 1, 
     "message": "Message" 
    }, 
    "customers": [ 
     { 
      "customerId": "182", 
      "customerName": "anil", 
      "email": "[email protected]", 
      "customerAbout": "To show the customer details", 
      "customerLogoSmall": "http://server.com/ thumb/1.jpg", 
      "customerLogoLarge": "http://server.com/ 2.jpg", 
      "ratingPoint": "10", 
      "address": "adress of customer", 
. 
. 
. 
      "openingHour": [ 
       { 
        "day": "monday", 
        "morning": "10to12.30", 
        "evening": "1to6", 
        "customerId": "182" 
       }, 
       { 
        "day": "sunday", 
        "morning": "", 
        "evening": "", 
        "customerId": "182" 
       } 
      ] 
     }, 
     { 
      "customerId": "183", 
      "customerName": "miche", 
      "email": "[email protected]", 
      "customerAbout": "To show the customer details", 
      "customerLogoSmall": "http://server.com/ thumb/1.jpg", 
      "customerLogoLarge": "http://server.com/ 2.jpg", 
      "ratingPoint": "10", 
. 
. 
. 
      "openingHour": [ 
       { 
        "day": "monday", 
        "morning": "10to12.30", 
        "evening": "1to6", 
        "customerId": "183" 
       }, 
       { 
        "day": "sunday", 
        "morning": "", 
        "evening": "", 
        "customerId": "183" 
       } 
      ] 
     } 
    ], 
    "allCustomers": [ 
     { 
      "customerId": "182" 
     }, 
     { 
      "customerId": "183" 
     } 
    ] 
} 

//CheckInCustomerOpeningHour.h ('키)

#import <Foundation/Foundation.h> 
#import <CoreData/CoreData.h> 
@class CheckInCustomers; 
@interface CheckInCustomerOpeningHour : NSManagedObject 
@property (nonatomic, retain) NSNumber * customerId; 
@property (nonatomic, retain) NSString * day; 
@property (nonatomic, retain) NSString * evening; 
@property (nonatomic, retain) NSString * morning; 
@property (nonatomic, retain) CheckInCustomers *openHourrelationship; 
@end 

//CheckInCustomers.h (위해'고객 '키)

#import <Foundation/Foundation.h> 
#import <CoreData/CoreData.h> 
@class CheckInCustomerOpeningHour; 
@interface CheckInCustomers : NSManagedObject 
@property (nonatomic, retain) NSString * address; 
@property (nonatomic, retain) NSNumber * alreadyRated; 
@property (nonatomic, retain) NSString * alreadyRatedMessage; 
@property (nonatomic, retain) NSNumber * avgCustomerRating; 
@property (nonatomic, retain) NSNumber * bonusProgram; 
@property (nonatomic, retain) NSString * colourCode; 
@property (nonatomic, retain) NSString * contactNumber; 
@property (nonatomic, retain) NSString * customerAbout; 
@property (nonatomic, retain) NSNumber * customerId; 
@property (nonatomic, retain) NSString * customerLogoLarge; 
@property (nonatomic, retain) NSString * customerLogoSmall; 
@property (nonatomic, retain) NSString * customerName; 
@property (nonatomic, retain) NSNumber * distanceToShop; 
@property (nonatomic, retain) NSString * email; 
@property (nonatomic, retain) NSString * location; 
@property (nonatomic, retain) NSString * locationLatitude; 
@property (nonatomic, retain) NSString * locationLongitude; 
@property (nonatomic, retain) NSNumber * numberOfCustomerRating; 
@property (nonatomic, retain) NSNumber * offerCount; 
@property (nonatomic, retain) NSNumber * rateLater; 
@property (nonatomic, retain) NSNumber * ratingPoint; 
@property (nonatomic, retain) CheckInCustomerOpeningHour *customerRelationShip; 
@end 

// CheckInAllCustomers .H

#import <Foundation/Foundation.h> 
#import <CoreData/CoreData.h> 
@interface CheckInAllCustomers : NSManagedObject 
@property (nonatomic, retain) NSNumber * customerId; 
@end 

('allCustomers'키의 경우) // 매핑 코드

RKObjectManager *objectManager = [[OPRestKit sharedDataManager] objectManager]; 

    RKObjectMapping *success_Mapping = [RKObjectMapping mappingForClass:[checkInSuccess class]]; 
    [success_Mapping mapKeyPath:@"code" toAttribute:@"code"]; 
    [success_Mapping mapKeyPath:@"message" toAttribute:@"message"]; 
    [objectManager.mappingProvider setMapping:success_Mapping forKeyPath:@"success"]; 


    RKManagedObjectMapping *openingHour_Mapping = [RKManagedObjectMapping mappingForClass:[CheckInCustomerOpeningHour class] 
                    inManagedObjectStore:objectManager.objectStore]; 
    openingHour_Mapping.primaryKeyAttribute = @"customerId"; 
    [openingHour_Mapping mapKeyPath:@"customerId" toAttribute:@"customerId"]; 
    [openingHour_Mapping mapKeyPath:@"day" toAttribute:@"day"]; 
    [openingHour_Mapping mapKeyPath:@"evening" toAttribute:@"evening"]; 
    [openingHour_Mapping mapKeyPath:@"morning" toAttribute:@"morning"]; 


    RKObjectMapping *hour_Mapping = [RKObjectMapping mappingForClass:[checkInOpeningHour class]]; 
    [hour_Mapping mapKeyPath:@"openingHour" toRelationship:@"openingHourData" withMapping:openingHour_Mapping]; 


    RKManagedObjectMapping *getCustomer_Mapping = [RKManagedObjectMapping mappingForClass:[CheckInCustomers class] 
                    inManagedObjectStore:objectManager.objectStore]; 
    getCustomer_Mapping.primaryKeyAttribute = @"customerId"; 
    [getCustomer_Mapping mapKeyPath:@"customerId" toAttribute:@"customerId"]; 
    [getCustomer_Mapping mapKeyPath:@"address" toAttribute:@"address"]; 
    [getCustomer_Mapping mapKeyPath:@"alreadyRated" toAttribute:@"alreadyRated"]; 
    [getCustomer_Mapping mapKeyPath:@"alreadyRatedMessage" toAttribute:@"alreadyRatedMessage"]; 
    [getCustomer_Mapping mapKeyPath:@"avgCustomerRating" toAttribute:@"avgCustomerRating"]; 
    [getCustomer_Mapping mapKeyPath:@"bonusProgram" toAttribute:@"bonusProgram"]; 
    [getCustomer_Mapping mapKeyPath:@"colourCode" toAttribute:@"colourCode"]; 
    [getCustomer_Mapping mapKeyPath:@"contactNumber" toAttribute:@"contactNumber"]; 
    [getCustomer_Mapping mapKeyPath:@"customerAbout" toAttribute:@"customerAbout"]; 
    [getCustomer_Mapping mapKeyPath:@"customerLogoLarge" toAttribute:@"customerLogoLarge"]; 
    [getCustomer_Mapping mapKeyPath:@"customerLogoSmall" toAttribute:@"customerLogoSmall"]; 
    [getCustomer_Mapping mapKeyPath:@"customerName" toAttribute:@"customerName"]; 
    [getCustomer_Mapping mapKeyPath:@"distanceToShop" toAttribute:@"distanceToShop"]; 
    [getCustomer_Mapping mapKeyPath:@"email" toAttribute:@"email"]; 
    [getCustomer_Mapping mapKeyPath:@"location" toAttribute:@"location"]; 
    [getCustomer_Mapping mapKeyPath:@"locationLatitude" toAttribute:@"locationLatitude"]; 
    [getCustomer_Mapping mapKeyPath:@"locationLongitude" toAttribute:@"locationLongitude"]; 
    [getCustomer_Mapping mapKeyPath:@"numberOfCustomerRating" toAttribute:@"numberOfCustomerRating"]; 
    [getCustomer_Mapping mapKeyPath:@"offerCount" toAttribute:@"offerCount"]; 
    [getCustomer_Mapping mapKeyPath:@"rateLater" toAttribute:@"rateLater"]; 
    [getCustomer_Mapping mapKeyPath:@"openingHour" toRelationship:@"customerRelationShip" withMapping:openingHour_Mapping]; 



    RKManagedObjectMapping *getAllCustomer_Mapping = [RKManagedObjectMapping mappingForClass:[CheckInAllCustomers class] 
                    inManagedObjectStore:objectManager.objectStore]; 
    [getAllCustomer_Mapping mapKeyPath:@"customerId" toAttribute:@"customerId"]; 

    RKObjectMapping *c_Mapping = [RKObjectMapping mappingForClass:[checkInRoot class]]; 
    [c_Mapping mapKeyPath:@"customers" toRelationship:@"customersData" withMapping:getCustomer_Mapping]; 
    [c_Mapping mapKeyPath:@"allCustomers" toRelationship:@"allCustomersData" withMapping:getAllCustomer_Mapping]; 

    [objectManager.mappingProvider registerMapping:c_Mapping withRootKeyPath:@""]; 

출력은 'openingHour'값이 NULL 인 '고객'과 'allCustomers'그러나 '성공'을 얻을 것이다.

[getCustomer_Mapping mapKeyPath:@"openingHour" toRelationship:@"customerRelationShip" withMapping:openingHour_Mapping]; 

은 위의 코드와 동일합니까? 내가 어떻게 openingHour '키'의 값을 얻는 것을 알고 DNT Plz은 나에게 도움이

편집 :.

작동 원리 설명해 보자. json으로 위

  • 우리의 서버에서 점점 모든
  • 'successIn'키는 'checkInSuccess'클래스 객체를 저장합니다.하지만, coredata는 필요 없습니다.
  • '고객'키는 배열입니다. '고객'키는 ' CheckInCustomers '객체
  • 'allCustomers'키가 다른 배열입니다. 그것은 'CheckInAllCustomers'객체로 coredata에 저장합니다.
  • 'openingHour'키는 배열입니다. 핵심 데이터에 저장해야합니다.
  • 'checkInOpeningHour'개체와 함께 coredata에 저장하지 않을 코드를 사용했습니다. 그것이 문제입니다.
  • 알고 싶습니다. 어떻게 열어서 '열어'배열을 저장합니까?
+0

어떻게, 당신은 SBJson, 또는 무언가를 사용하여 JSON을 구문 분석하는거야 ?? – rptwsthi

+0

RestKit을 사용 중입니다. –

+0

RSKit은 JSON을 구문 분석 한 후 NSDictionary를 반환합니까 ?? – rptwsthi

답변

0

은 어떻게 작동 참조 : valueForKey에 의해 반환되는 값은 DICT 또는 배열 인 경우 여기

NSDictionary *dictionary = [yourParser parse: jsonString]; 
for (int i= 0; i < [[dictionary valueForKey:@"customers"] count]; i++) { 
    NSArray *openingHourArray = [[[dictionary valueForKey:@"customers"] objectAtIndex:i] valueForKey: openingHour]; 

    for(int j = 0; j <[openingHourArray count]; j++){ 
     NSDictionary *openingHourDictionary = [openingHourArray objectAtIndex:i]; 
     NSLog(@"openingHourDictionary =%@", openingHourDictionary); 
    } 

} 

당신의 숙제

이, 그에 따라 확인합니다. 그렇지 않으면 응용 프로그램이 중단됩니다. 해피 Coading .. :)

+0

그게 뭔데 .. 결코 우리가 사용하고 있습니다. 가치 키를 얻는 coredata에 저장해야합니다. 'openingHour' –

+0

JSON 데이터 사전에서 영업 시간 사전을 얻을 수있는 방법입니다. 도움이되지 않으면 실제로 원하는 내용을 설명해주세요. – rptwsthi

+0

RestKit을 알고 있습니까?예, 설명해 드리겠습니다. –