2013-12-12 43 views
-1
轉換對象到JSON

我有一個名爲 「商店」 像一個主要的實體類:問題:在iOS的

Store.h: -

#import <Foundation/Foundation.h> 
#import "SignIn.h" 

@interface Store : NSObject 

@property (nonatomic, retain) NSString *storeId; 
@property (nonatomic, retain) NSString *storeProfileId; 
@property (nonatomic, retain) NSString *storeName; 
@property (nonatomic, retain) NSString *storeRegion; 

@property (nonatomic, retain) SignIn *signIn; 

@end 

Store.m: -

#import "Store.h" 

@implementation Store 

@synthesize storeId, storeProfileId, storeName, storeRegion, signIn; 

- (id) initWithCoder: (NSCoder *)coder 
{ 
    self = [[Store alloc] init]; 
    if (self != nil) 
    { 
     self.storeId = [coder decodeObjectForKey:@"storeId"]; 
     self.storeProfileId = [coder decodeObjectForKey:@"storeProfileId"]; 
     self.storeName = [coder decodeObjectForKey:@"storeName"]; 
     self.storeRegion = [coder decodeObjectForKey:@"storeRegion"]; 

     self.signIn = [coder decodeObjectForKey:@"signIn"]; 
    } 
    return self; 
} 

- (void)encodeWithCoder: (NSCoder *)coder 
{ 
    [coder encodeObject:storeId forKey:@"storeId"]; 
    [coder encodeObject:storeProfileId forKey:@"storeProfileId"]; 
    [coder encodeObject:storeName forKey:@"storeName"]; 
    [coder encodeObject:storeRegion forKey:@"storeRegion"]; 

    [coder encodeObject:signIn forKey:@"signIn"]; 
} 

@end 

在Store類中,我正在使用另一個類名「Sign In」,其中包含一些其他屬性。

SignIn.h: -

#import <Foundation/Foundation.h> 

@interface SignIn : NSObject 

@property (nonatomic, retain) NSString *inTime; 
@property (nonatomic, retain) NSString *outTime; 
@property (nonatomic, retain) NSString *isStatus; 

@end 

SignIn.m: -

#import "SignIn.h" 

@implementation SignIn 
@synthesize inTime, outTime, isStatus; 

- (id) initWithCoder: (NSCoder *)coder 
{ 
    self = [[SignIn alloc] init]; 
    if (self != nil) 
    { 
     self.inTime = [coder decodeObjectForKey:@"inTime"]; 
     self.outTime = [coder decodeObjectForKey:@"outTime"]; 
     self.isStatus = [coder decodeObjectForKey:@"isStatus"]; 
    } 
    return self; 
} 

- (void)encodeWithCoder: (NSCoder *)coder 
{ 
    [coder encodeObject:inTime forKey:@"inTime"]; 
    [coder encodeObject:outTime forKey:@"outTime"]; 
    [coder encodeObject:isStatus forKey:@"isStatus"]; 
} 

@end 

現在我需要張貼在服務器此店鋪對象。所以我用下面的代碼創建詞典:

NSMutableArray *storeJSONArray=[NSMutableArray array]; 
    for (Store *store in array1) { 

     NSMutableDictionary *storeJSON=[NSMutableDictionary dictionary]; 

     [storeJSON setValue:store.storeId forKey:@"storeId"]; 
     [storeJSON setValue:store.storeProfileId forKey:@"storeProfileId"]; 
     [storeJSON setValue:store.storeName forKey:@"storeName"]; 
     [storeJSON setValue:store.storeRegion forKey:@"storeRegion"]; 


     //Sign In 
     [storeJSON setValue:store.signIn.inTime forKey:@"inTime"]; 
     [storeJSON setValue:store.signIn.outTime forKey:@"outTime"]; 
     [storeJSON setValue:store.signIn.isStatus forKey:@"isStatus"]; 

     [storeJSONArray addObject:storeJSON]; 
    } 

    NSMutableDictionary *dictionnary = [NSMutableDictionary dictionary]; 
    [dictionnary setObject:storeJSONArray forKey:@"StoreRequest"]; 

    NSError *error = nil; 
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionnary 
                 options:kNilOptions 
                 error:&error]; 

    NSString *urlString [email protected]"http://...................php"; 

    NSURL *url = [NSURL URLWithString:urlString]; 
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; 

    [request setHTTPMethod:@"POST"]; 

    [request setHTTPBody:jsonData]; 
    NSURLResponse *response = NULL; 
    NSError *requestError = NULL; 

    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&requestError]; 

    NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding] ; 

,但我沒有得到正確的JSON,你可以請檢查我的代碼,讓我知道是我的錯。提前致謝。

+1

不清楚你在問什麼。 –

+0

@Virussmcahere是我的一個名爲「Store」的實體類,只有我需要將此類對象轉換爲JSON。但我不知道我在哪裏犯錯。 –

+0

那麼它給錯誤? –

回答

1

但是我沒有得到正確的JSON,你可以請檢查我的代碼,讓我知道我的錯誤在哪裏。

問題是你沒有在任何地方創建對象的JSON表示;你只是在創建一本字典。字典可以轉換爲JSON(假設它們只包含某些類型的數據),但它們本身不是JSON - 它們是Objective-C對象。你可能想添加一個調用,比如:

NSError *error = nil; 
NSData *json = [NSJSONSerialization dataWithJSONObject:dictionnary options:0 error:&error]; 

你告訴我們你的兩個類的NSCoding方法,但是你應該明白,NSJSONSerialization不依賴於NSCoding,所以沒有代碼的打算參加進來。

更新:修改您的示例包括NSJSONSerialization後,你說你得到JSON看起來像這樣:

{"StoreRequest":[{"signOutStatus":false,"greetingStatus":false,"isBackFromVisit"‌​‌​:false,"digitalMerchandisingStatus":false,"feedbackStatus":false,"storeRegion":‌​"B‌​ishan Junction 8","isSubmit":false,"storeName":"Best Denki","storeId":"SG-2","planVisitStatus":false,"storeProfileId":5,"merchandisin‌​‌​gStatus":false}]} 

這似乎是正確的,因爲你的值相加dictionnary。但是你說你想要

{ "Checklist": [ { "vm_code": "SGVM0001", "store_id": "SG-12", "store_name": "Best Denki", "store_address": "Ngee Ann City", "visit_date": { "date": "2013-12-04 00:00:00", "timezone_type": 3, "timezone": "Asia/Calcutta" } "sign_in": { "date": "2013-12-05 11:03:00", "timezone_type": 3, "timezone": "Asia/Calcutta" }] 

這完全不符合,你傳遞給NSJSONSerialization的對象。所以,這裏的問題是你提供了不正確的數據給NSJSONSerialization。

+0

@CalebThanks其實NSCoding,我已經採取在他們的領域顯示價值。我正在使用NSJSONSerialization但仍未顯示。 –

0

NSError *error; 
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionaryOrArrayToOutput 
                options:kNilOptions // Pass 0 if you don't care about the readability of the generated string 
                error:&error]; 

if (! jsonData) { 
    NSLog(@"Got an error: %@", error); 
} else { 
    NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; 
} 
1

嘗試,如果你在iOS 5以上的工作,那麼你可以使用NSJSONSerialization

NSData *data= [NSJSONSerialization dataWithJSONObject:storeJSONArray 
                options:NSJSONWritingPrettyPrinted 
                error:nil]; 
if (data) 
{ 
    NSString *json = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; 
    NSLog(@"JSON : %@",json); 
} 
+0

@ MidhunThanks..Yes,我正在iOS 5+上工作。請檢查我更新的問題。我使用php url在服務器上發佈一個對象。 –

+0

@AnandGautam:你得到的JSON格式是什麼? –

+0

@MidhunMP它只在JSON中顯示一個範圍。 –

1

您應該將您的NSMutableDictionary序列化爲JSON。 您可以通過使用NSJSONSerialization做到這一點:

NSError *error; 
NSData *myData = [NSJSONSerialization dataWithJSONObject:storeJSON options:0 error:&error]; 
NSString *myJSON = [[NSString alloc] initWithBytes:[myData bytes]]; 

這應該給你的JSON。