In general, I mean that you pass the class and JSON to the method, and this method converts the JSON data into your object based on the class definition.I am interested in both conceptual answers and Objective-C approaches.
I assume that with a common parser, you mean an implicit JSON-to-object resolver.
In this case, there is one problem. Which may or may not be solvable depends on whether the variables are types in Objective-C (actually this is a shame that I don't know). While its rather easy-to-match setter method names (assuming Objective-C provides reflection) to jason attributes, it's hard to determine which object to create for nested jason objects. Therefore, you need a factory view that suggests what types of nested objects may have. If variables in Objective-C are types that can be used as a hint if you are not left on their own.
, /. , JSON. : JSON . : JSON uniq-id, .
Objective-C, , , JSON . classInstance.setJsonAttribute(key, value); JSON, JSON, uniq-id, : , setJsonAttribute. , .
classInstance.setJsonAttribute(key, value);
, , , .
Class c = [self class];
C, , , JSON.
. JSON
var x = { prop1: "Test 1", prop2: "Test 2};
@interface x : NSObject { NSString* prop1; NSString* prop2; } @property (nonatomic,retain) NSString* prop1; @property (nonatomic,retain) NSString* prop2;
, .
, :
- (NSObject)mapClassFromJSON:(NSObject)class fromJSON:(id)jsonArray { //code needs to iterate over the jsonArray (needs to check using isKindOfClass if it an NSArray or NSDictionary) and assign the class properties to it. }
, SBJSON, , JSON NSArray NSDictionary, , . , smarts, .
:
http://developer.apple.com/mac/library/documentation/Cocoa/Reference/ObjCRuntimeRef/Reference/reference.html#//apple_ref/c/func/class_copyPropertyList
, , ...
NSArray .. , , json-framework
():
#import <Cocoa/Cocoa.h> #import "JSON/JSON.h" int main(int argc, char *argv[]) { NSLog(@"%@", [@"[ 100, 500, 300, 200, 400 ]" JSONValue]); return 0; }
, - . json-framework, , . , , , :).
, , ! , .
https://github.com/RestKit/RestKit
https://github.com/dchohfi/KeyValueObjectMapping
I don't know objective-c at all, but I know JSON.
This is a single format for exchanging data with RFC, so you can definitely play your own parser. Does objective-c support generic types? If so, then your search will be achievable, and id will also suggest that someone else has everything ready collapsed.
Source: https://habr.com/ru/post/1725380/More articles:making the revision revision number visible in my R scripts - svnShibboleth + Facebook - facebookHow to disable TAction.Shortcut or TMenuItem.Shortcut? - delphigetting the highest version of subversion in my file `R CMD build` filename - bashUse pyExcelerator to create a dynamic Excel file using Django. Providing a unique temporary file name - pythonUpdate a single value from a list of dependent objects - javarun application after installshield patch - patchIs ASP.NET MVC required to reboot the server? - installationWhat protocols exist that can be used with WCF? - c #BlackBerry - Get the current process ID - processAll Articles