How to convert JSON to Object

后端 未结 3 725
南旧
南旧 2021-02-05 23:47

I defined some custom classes, such as Teacher, Student... Now I receive teacher info (JSON string) from remote server.

How can I convert the J

3条回答
  •  鱼传尺愫
    2021-02-06 00:40

    These are all good frameworks for JSON parsing to dictionaries or other primitives, but if you're looking to avoid doing a lot of repetitive work, check out http://restkit.org . Specifically, check out https://github.com/RestKit/RestKit/blob/master/Docs/Object%20Mapping.md This is the example on Object mapping where you define mapping for your Teacher class and the json is automagically converted to a Teacher object by using KVC. If you use RestKit's network calls, the process is all transparent and simple, but I already had my network calls in place and what I needed was to convert my json response text to a User object (Teacher in your case) and I finally figured out how. If that's what you need, post a comment and I'll share how to do it with RestKit.

    Note: I will assume the json is output using the mapped convention {"teacher": { "id" : 45, "name" : "Teacher McTeacher"}}. If it's not this way, but instead like this {"id" : 45, "name" : "Teacher McTeacher"} then don't worry ... object mapping design doc in the link shows you how to do this...a few extra steps, but not too bad.

    This is my callback from ASIHTTPRequest

    - (void)requestFinished:(ASIHTTPRequest *)request {
        id parser = [[RKParserRegistry sharedRegistry] parserForMIMEType:[request.responseHeaders valueForKey:@"Content-Type"]]; // i'm assuming your response Content-Type is application/json
        NSError *error;
        NSDictionary *parsedData = [parser objectFromString:apiResponse error:&error];
        if (parsedData == nil) {
            NSLog(@"ERROR parsing api response with RestKit...%@", error);
            return;
        }
    
        [RKObjectMapping addDefaultDateFormatterForString:@"yyyy-MM-dd'T'HH:mm:ssZ" inTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]]; // This is handy in case you return dates with different formats that aren't understood by the date parser
    
        RKObjectMappingProvider *provider = [RKObjectMappingProvider new];
    
        // This is the error mapping provider that RestKit understands natively (I copied this verbatim from the RestKit internals ... so just go with it
        // This also shows how to map without blocks
        RKObjectMapping* errorMapping = [RKObjectMapping mappingForClass:[RKErrorMessage class]];
        [errorMapping mapKeyPath:@"" toAttribute:@"errorMessage"];
        [provider setMapping:errorMapping forKeyPath:@"error"];
        [provider setMapping:errorMapping forKeyPath:@"errors"];
    
        // This shows you how to map with blocks
        RKObjectMapping *teacherMapping = [RKObjectMapping mappingForClass:[Teacher class] block:^(RKObjectMapping *mapping) {
            [mapping mapKeyPath:@"id" toAttribute:@"objectId"];
            [mapping mapKeyPath:@"name" toAttribute:@"name"];
        }];
    
        [provider setMapping:teacherMapping forKeyPath:@"teacher"];
    
        RKObjectMapper *mapper = [RKObjectMapper mapperWithObject:parsedData mappingProvider:provider];
        Teacher *teacher = nil;
        RKObjectMappingResult *mappingResult = [mapper performMapping];
        teacher = [mappingResult asObject];
    
        NSLog(@"Teacher is %@ with id %lld and name %@", teacher, teacher.objectId, teacher.name);
    }
    

    You can obviously refactor this to make it cleaner, but that now solves all my problems.. no more parsing... just response -> magic -> Object

提交回复
热议问题