Is it possible to create a common JSON parser?

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.

+3
source share
6 answers

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.

+1
source

, /. , JSON. : JSON . : JSON uniq-id, .

Objective-C, , , JSON . classInstance.setJsonAttribute(key, value); JSON, JSON, uniq-id, : , setJsonAttribute. , .

0

, , , .

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

, , ...

0

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, , . , , , :).

0

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.

-1
source

Source: https://habr.com/ru/post/1725380/


All Articles