Convert an object into Json using SBJson or other JSON library
2012-04-04 12:54
429 查看
Using
The .h file,
SBJson, to convert a object to JSON string, you have to override the
proxyForJsonmethod. Like the following,
The .h file,
@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; }In the implementation file,
- (id) proxyForJson { return [NSDictionary dictionaryWithObjectsAndKeys: receiverFirstName, @"ReceiverFirstName", receiverMiddleInitial, @"ReceiverMiddleInitial", receiverLastName, @"ReceiverLastName", receiverLastName2, @"ReceiverLastName2", nil ]; }And to get the object from the JSON string you have to write a
parseResponsemethod like this,
- (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; }
相关文章推荐
- fatal error C1047: The object or library file "..."was created with an older compiler than other obj
- The object or library file 'xxx.obj' was created with an older compiler than other obj
- fatal error C1047: The object or library file "..."was created with an older compiler than
- post a json object with an array using curl
- Gson is a Java library that can be used to convert Java Objects into their JSON representation.
- Convert string to JSONObject by net.sf.json library
- Convert an ABAP table into XML file using SAP DOM Approach
- Serialize an Object Using JSON
- Convert an XML file into an ABAP table using SAP DOM Approach
- Conversion between json and object using SBJson lib
- [Ramda] Convert a QueryString to an Object using Function Composition in Ramda
- Conversion between json and object using SBJson lib
- Convert a console project into a Win32 GUI project or the other way around.
- Conversion between json and object using SBJson lib
- javascript: Jquery each loop with json array or object
- dojo.widget' is null or not an object
- The type or namespace name '***' could not be found (are you missing a using directive or an asse
- convert Java object to / from JSON
- .....an attempt to load the C runtime library without using a manifest.....
- oracle alert出现Memory Notification: Library Cache Object loaded into SGA