如何获取关系键 (涉及IT技术,翻译为问题标题,无需回答)

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

//检查所有客户端.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”键的值。请帮帮我。

编辑:

让我解释一下它的工作原理:

  • 上面的JSON数据是从我们的服务器获取的,需要将所有键保存到coredata中。
  • 'success'键是对象而不是数组,并且将保存为'checkInSuccess'类对象,但不需要在coredata中保存。
  • 'customers'键是数组。它将使用'CheckInCustomers'对象保存到coredata中。
  • 'allCustomers'键是另一个数组。它将使用'CheckInAllCustomers'对象保存到coredata中。
  • 'openingHour'键是一个数组。我们需要将其保存到coredata中。
  • 我使用的代码不会使用'checkInOpeningHour'对象保存到coredata中。这是个问题。
  • 我想知道如何将“openingHour”数组保存到coredata中?

你是如何解析JSON的?使用类似SBJson这样的工具吗? - rptwsthi
RSKit解析JSON后是否返回NSDictionary? - rptwsthi
@rptwsthi 是的... 有什么问题吗? - Nag Raja
很好,没问题,事实上问题已经解决了。 - rptwsthi
但是获取customerRelationShip为空。事实上? - Nag Raja
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返回的值是字典还是数组,否则会导致应用程序崩溃。 编程愉快.. :)


那是什么?我们从未使用过。我需要将值键为“openingHour”的数据存储到CoreData中。 - Nag Raja
通过这种方式,您可以从JSON数据字典中获取开放时间字典。如果这并没有帮助到您,请详细解释您实际想要的是什么。 - rptwsthi
你知道RestKit吗?如果是,我会解释它。 - Nag Raja
向他人解释它。 :) - rptwsthi
1
抱歉大家。我的代码是正确的。我另一个代码中有一行出了问题,已经修复了。 - Nag Raja
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接