我想用entityset
创建json对象{
"$id": "1",
"EntitySet": [
{
"$id": "2",
"id": 1,
"title1": "Mr"
},
{
"$id": "3",
"id": 2,
"title1": "Ms"
},
{
"$id": "4",
"id": 3,
"title1": "Dr"
},
{
"$id": "5",
"id": 4,
"title1": "Mrs"
},
{
"$id": "6",
"id": 5,
"title1": "N/A"
},
{
"$id": "7",
"id": 6,
"title1": "Other"
}
],
"OperationStatus": true,
"OperationMessage": "Records Available",
"RowsEffected": 0
}
使用create new class存储到数组中?有可能..?
答案 0 :(得分:-1)
我认为你的JSON应该是这个......
{
"$id": "1",
"EntitySet": [
{
"$id": "2",
"id": 1,
"title1": "Mr"
},
{
"$id": "3",
"id": 2,
"title1": "Ms"
},
{
"$id": "4",
"id": 3,
"title1": "Dr"
},
{
"$id": "5",
"id": 4,
"title1": "Mrs"
},
{
"$id": "6",
"id": 5,
"title1": "N/A"
},
{
"$id": "7",
"id": 6,
"title1": "Other"
}
],
"OperationStatus": true,
"OperationMessage": "Records Available",
"RowsEffected": 0
}
在顶层,这是一个字典,其中一个键“EntitySet”指向一个数组。
把它变成一个对象......
NSDictionary *jsonDictionary = [NSJSONSerialization JSONObjectWithData:theJSONData options:0 error:nil];
此jsonDictionary
将具有1个键/值对,其中键为“EntitySet”,值为字典数组。
修改强>
更新JSON之后,唯一改变的是字典将具有多个键/值对。
但其他一切都是一样的。
编辑2
然后,您可以将其保存到一个看起来像这样的新类......
@interface MyModelClass : NSObject
@property NSString *id;
@property NSArray *entities;
@property (nonatomic, assign) BOOL operationStatus;
@property NSString *operationMessage;
@property NSNumber *rowsEffected;
- (id)initWithDictionary:(NSDictionary *)dictionary;
@end
然后实现看起来像......
#import "MyModelClass.h"
@implementation MyModelClass
- (id)initWithDictionary:(NSDictionary *)dictionary
{
self = [super init];
if (self) {
self.id = dictionary[@"$id"];
self.entities = dictionary[@"EntitySet"];
// and so on...
}
return self;
}
@end
然后,当您在上面创建jsonDictionary
时,您可以获得类似......
MyModelClass *myObject = [[MyModelClass alloc] initWithDictionary:jsonDictionary];
答案 1 :(得分:-1)
您可以从解析的NSDictory json数据生成Api。
//JsonModel.h Start
#import <Foundation/Foundation.h>
@class ModelEntitySet;
@interface Api : NSObject
@property(nonatomic,retain) NSString * id;
@property(nonatomic,retain) NSMutableArray * EntitySet;
@property(nonatomic,retain) NSString * OperationStatus;
@property(nonatomic,retain) NSString * OperationMessage;
@property(nonatomic,retain) NSNumber * RowsEffected;
+ (id) objectWithDictionary:(NSDictionary*)dictionary;
- (id) initWithDictionary:(NSDictionary*)dictionary;
@end
@interface ModelEntitySet : NSObject
@property(nonatomic,retain) NSString * Sid;
@property(nonatomic,retain) NSNumber * Id;
@property(nonatomic,retain) NSString * Title1;
+ (id) objectWithDictionary:(NSDictionary*)dictionary;
- (id) initWithDictionary:(NSDictionary*)dictionary;
@end
//JsonModel.h End
// JsonModel.m Start
#import "JsonModel.h"
@implementation Api
@synthesize id;
@synthesize EntitySet;
@synthesize OperationStatus;
@synthesize OperationMessage;
@synthesize RowsEffected;
+ (id) objectWithDictionary:(NSDictionary*)dictionary
{
id obj = [[Api alloc] initWithDictionary:dictionary];
return obj;
}
- (id) initWithDictionary:(NSDictionary*)dictionary
{
self=[super init];
if(self)
{
id = [dictionary objectForKey:@"$id"];
NSArray* temp = [dictionary objectForKey:@"EntitySet"];
EntitySet = [[NSMutableArray alloc] init];
for (NSDictionary *d in temp) {
[EntitySet addObject:[ModelEntitySet objectWithDictionary:d]];
}
OperationStatus = [dictionary objectForKey:@"OperationStatus"];
OperationMessage = [dictionary objectForKey:@"OperationMessage"];
RowsEffected = [dictionary objectForKey:@"RowsEffected"];
}
return self;
}
@end
@implementation ModelEntitySet
@synthesize Sid;
@synthesize Id;
@synthesize Title1;
+ (id) objectWithDictionary:(NSDictionary*)dictionary
{
id obj = [[[ModelEntitySet alloc] initWithDictionary:dictionary] autorelease];
return obj;
}
- (id) initWithDictionary:(NSDictionary*)dictionary
{
self=[super init];
if(self)
{
Sid = [dictionary objectForKey:@"$id"];
Id = [dictionary objectForKey:@"Id"];
Title1 = [dictionary objectForKey:@"Title1"];
}
return self;
}
@end
//JsonModel.m End