2011-07-27 94 views
2

我需要一個易於使用的庫whit例子,用於將NSObject轉換爲JSON並再次返回,我在網絡上發現了大量解析JSon的解析示例,但不是太多使用SBJSON將NSObject轉換爲JSON,Anybody body有一個很好的教程或將NSObject轉換爲JSON的示例代碼?使用SBJson或其他JSON庫將對象轉換爲Json

回答

12

使用SBJson,要將對象轉換爲JSON字符串,必須重寫proxyForJson方法。像下面,

.h文件,

@interface MyCustomObject : NSObject { 
    NSString *receiverFirstName; 
    NSString *receiverMiddleInitial; 
    NSString *receiverLastName; 
    NSString *receiverLastName2; 
} 
@property (nonatomic, retain) NSString *receiverFirstName; 
@property (nonatomic, retain) NSString *receiverMiddleInitial; 
@property (nonatomic, retain) NSString *receiverLastName; 
@property (nonatomic, retain) NSString *receiverLastName2; 

- (id) proxyForJson; 
- (int) parseResponse :(NSDictionary *) receivedObjects; 
} 

在實現文件中,

- (id) proxyForJson { 

     return [NSDictionary dictionaryWithObjectsAndKeys: 
      receiverFirstName, @"ReceiverFirstName", 
      receiverMiddleInitial, @"ReceiverMiddleInitial", 
      receiverLastName, @"ReceiverLastName", 
      receiverLastName2, @"ReceiverLastName2", 
      nil ]; 
    } 

,並從JSON字符串獲取對象,你必須寫一個parseResponse方法是這樣,

- (int) parseResponse :(NSDictionary *) receivedObjects { 
    self.receiverFirstName = (NSString *) [receivedObjects objectForKey:@"ReceiverFirstName"]; 
    self.receiverLastName = (NSString *) [receivedObjects objectForKey:@"ReceiverLastName"]; 

    /* middleInitial and lastname2 are not required field. So server may return null value which 
    eventually JSON parser return NSNull. Which is unrecognizable by most of the UI and functions. 
    So, convert it to empty string. */ 
    NSString *middleName = (NSString *) [receivedObjects objectForKey:@"ReceiverMiddleInitial"]; 
    if ((NSNull *) middleName == [NSNull null]) { 
     self.receiverMiddleInitial = @""; 
    } else { 
     self.receiverMiddleInitial = middleName; 
    } 

    NSString *lastName2 = (NSString *) [receivedObjects objectForKey:@"ReceiverLastName2"]; 
    if ((NSNull *) lastName2 == [NSNull null]) { 
     self.receiverLastName2 = @""; 
    } else { 
     self.receiverLastName2 = lastName2; 
    } 

    return 0; 
} 
13

與SBJSON,它非常簡單。

NSString *myDictInJSON = [myDict JSONRepresentation]; 
NSString *myArrayInJSON = [myArray JSONRepresentation]; 

當然,走另一條路數組,這樣做:

NSDictionary *myDict = [myDictInJSON JSONValue]; 
NSArray *myArray = [myArrayInJSON JSONValue]; 
+0

母豬它只接受字典或數組的轉換?如果是的話,是一種方法來轉換一個NSObject的dictonary或我必須創建manualy? – Radu

+1

默認情況下,我相信如此。從邏輯上講,它也應該接受鑄造的子類,就像這樣:'[(NSDictionary *)myCustomDict JSONRepresentation]',只要你的子類沒有對類的基礎做任何改變。 –

+1

然而,它不會在任何任意的NSObject上工作,因爲NSObject不是(必然)一組鍵/值對或列表。爲了做到這一點,我會建議迭代NSObject的值,並使NSDictionary不在此範圍內。或者,如果你是NSObject的子類化,可能考慮有一個方法'-dictionaryForm',它可以做同樣的事情 –

2

從JSON字符串對象:

SBJsonParser *parser = [[SBJsonParser alloc] init]; 

// gives array as output 

id objectArray = [parser objectWithString:@"[1,2,3]"]; 

// gives dictionary as output 

id objectDictionary = [parser objectWithString:@"{\"name\":\"xyz\",\"email\":\"[email protected]\"}"]; 

從對象到JSON字符串:

SBJsonWriter *writer = [[SBJsonWriter alloc] init]; 

id *objectArray = [NSArray arrayWithObjects:@"Hello",@"World", nil]; 

// Pass an Array or Dictionary object. 

id *jsonString = [writer stringWithObject:objectArray]; 
相關問題