Iphone 如何获取关系密钥
对不起,英语:(……我知道用restkit编写简单的json。但是非常混乱的json如下所示Iphone 如何获取关系密钥,iphone,ios,xcode,ipad,restkit,Iphone,Ios,Xcode,Ipad,Restkit,对不起,英语:(……我知道用restkit编写简单的json。但是非常混乱的json如下所示 { "success": { "code": 1, "message": "Message" }, "customers": [ { "customerId": "182&quo
{
"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”键)
输出将获得“success”、“customers”和“allCustomers”,但“openingHour”值为空
[getCustomer_Mapping mapKeyPath:@"openingHour" toRelationship:@"customerRelationShip" withMapping:openingHour_Mapping];
像上面的代码一样正确吗?我不知道如何获取“openingHour”键的值。请帮助我
编辑:
让我解释一下它是如何工作的:
- 上面的json来自我们的服务器。所有密钥都需要保存到coredata中
- “成功”键是对象而不是数组。它将保存“checkInSuccess”类对象,但不需要coredata
- “customers”键是数组。它将使用“CheckInCustomers”对象保存到coredata
- “allCustomers”键是另一个数组。它将使用“CheckInAllCustomers”对象保存到coredata
- “openingHour”键是一个数组。我们需要保存到核心数据
- 我使用的代码不会保存到带有“checkInOpeningHour”对象的coredata。这就是问题所在
- 我想知道如何在coredata中保存“openingHour”数组
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);
}
}
NSDictionary*dictionary=[yourParser parse:jsonString];
对于(int i=0;i<[dictionary valueForKey:@“customers”]计数];i++){
NSArray*openingHourArray=[[[dictionary valueForKey:@“customers”]objectAtIndex:i]valueForKey:openingHour];
对于(int j=0;j如何解析JSON,使用SBJson之类的东西?RSKit在解析JSON后是否返回NSDictionary???@rptwsthi是…有问题吗?很好,没有问题infacr问题解决:但是customerRelationShip getting为NULL。infacr?这是什么…我们从未使用过。我需要存储到coredata获取值的键是“openingHour”通过这个,你可以从你的JSON数据字典中获得开放时间字典。如果没有帮助,那么请详细解释你真正想要什么。你知道RestKit吗?如果知道,我会向其他人解释…:)对不起,大家。上面我的代码是正确的。我的另一个代码中有一行的问题。这已经修复。
#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:@""];
[getCustomer_Mapping mapKeyPath:@"openingHour" toRelationship:@"customerRelationShip" withMapping:openingHour_Mapping];
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);
}
}