3

英語で申し訳ありません:(....私はrestkitで単純なjsonをコーディングすることを知っています。しかし、非常に紛らわしいjsonは以下のようなものです。

 {
    "success": {
        "code": 1,
        "message": "Message"
    },
    "customers": [
        {
            "customerId": "182",
            "customerName": "anil",
            "email": "anil@yahoo.com",
            "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": "babu@yahoo.com",
            "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('openingHour'キーの場合)

#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('customers'キーの場合)

#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('allCustomers'キーの場合)

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

//マッピングコード

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:@""];

出力は「success」、「customers」、および「allCustomers」を取得しますが、「openingHour」の値はNULLです。

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

上記のコードのように正しいですか?「openingHour」キーの値を取得する方法がわかりません。Plzが役に立ちます

編集:

それがどのように機能するかを説明させてください:

  • 上記のjsonはサーバーから取得しています。すべてのキーはcoredataに保存する必要があります。
  • 'success'キーは、配列ではなくオブジェクトです。'checkInSuccess'クラスオブジェクトを保存しますが、coredataは必要ありません。
  • 「顧客」キーは配列です。'CheckInCustomers'オブジェクトを使用してcoredataに保存されます。
  • 'allCustomers'キーは別の配列です。'CheckInAllCustomers'オブジェクトを使用してcoredataに保存されます。
  • 'openingHour'キーは配列です。コアデータに保存する必要があります。
  • 'checkInOpeningHour'オブジェクトでcoredataに保存されないコードを使用しました。それが問題です。
  • 'openingHour'配列をcoredataに保存する方法を知りたいですか?
4

1 に答える 1

0

仕組みを見る:

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);
    }

}

ここで宿題です。返される値valueForKeyが dict または array であるかどうかを確認してください。そうしないと、アプリケーションがクラッシュする可能性があります。幸せなコーディング.. :)

于 2012-12-16T11:16:12.057 に答える