2014-02-12 359 views
-1

我想創建JSON對象與EntitySet的解析URL以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 
} 

這些被存儲到與創建新類數組...?可能嗎..?

+0

ü想提取我想創建新類詞典或別的東西 –

+0

是有可能的是 – Chenna223

+0

存儲的JSONObject * OBJ = JSONObject的objectatindex(I) – Chenna223

回答

-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的」和值是字典的陣列。

EDIT

更新JSON改變的唯一事情之後是該字典將有超過1個鍵/值對。

其他的都一樣。

EDIT 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]; 
+0

-1她/他要求的課程不是字典。先閱讀問題。 – csk

+0

@Cemsi哈哈,好吧,編輯... – Fogmeister

+0

@Cemsi哈哈,現在更新。請刪除投票。 – Fogmeister

-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 
+0

-1如果你打算向某人推薦一個類定義和接口,那麼至少應該使命名約定正確。此外,使用ARC將無法使用(所有保留)。而且你使用的iVars屬性非常古老。而'operationStatus'是'NSNumber'而不是'NSString'。 – Fogmeister

+0

是的你的權利。我的不好 – csk

+0

真的嗎?你刪除了屬性?你應該真的只是使用屬性而不是iVars。您可以更好地控制使用屬性的ARC內存管理。 – Fogmeister