Json与字典、数组之间的转换

pragma mark - NSArray to String


+ (NSString *)jsonStringForArray:(NSArray *)array {
  if (![array isKindOfClass:[NSArray class]]) {
        return nil;
    }

    @try {
        NSError *error = nil;
        // iOS5中,苹果引入了一个解析JSON串的NSJSONSerialization类
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:array options:kNilOptions error:&error];
        if (!jsonData || error) {
            NSLog(@"JSONSerialization Error:%@", error);
            return nil;
        }

        NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
        if ([jsonString isKindOfClass:[NSString class]]) {
            return jsonString;
        }
    }
    @catch (NSException *exception) {
        NSLog(@"%@", exception);
    }

    return nil;
}

pragma mark - Dictionary to Data/String


+ (NSString *)jsonStringForDictionary:(NSDictionary *)dictionary {
    if (![dictionary isKindOfClass:[NSDictionary class]]) {
        return nil;
    }

    @try {
        NSError *error = nil;
        // iOS5中,苹果引入了一个解析JSON串的NSJSONSerialization类
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionary options:kNilOptions error:&error];
        if (!jsonData || error) {
            NSLog(@"JSONSerialization Error:%@", error);
            return nil;
        }

        NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
        if ([jsonString isKindOfClass:[NSString class]]) {
            return jsonString;
        }
    }
    @catch (NSException *exception) {
        NSLog(@"%@", exception);
    }

    return nil;
}


+ (NSData *)jsonDataForDictionary:(NSDictionary *)dictionary {
    if (![dictionary isKindOfClass:[NSDictionary class]]) {
        return nil;
    }

    @try {
        NSError *error = nil;
        // iOS5中,苹果引入了一个解析JSON串的NSJSONSerialization类
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionary options:kNilOptions error:&error];
        if (!jsonData || error) {
            NSLog(@"JSONSerialization Error:%@", error);
            return nil;
        }

        if ([jsonData isKindOfClass:[NSData class]]) {
            return jsonData;
        }
    }
    @catch (NSException *exception) {
        NSLog(@"%@", exception);
    }

    return nil;
}

pragma mark - Data/String to Dictionary


+ (NSDictionary *)jsonDictionaryForData:(NSData *)jsonData {
    if (![jsonData isKindOfClass:[NSData class]]) {
        return nil;
    }

    @try {
        NSError *error = nil;
        // iOS5中,苹果引入了一个解析JSON串的NSJSONSerialization类
        id jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:&error];
        if (!jsonObject || error) {
            NSLog(@"JSONSerialization Error:%@", error);
            return nil;
        }

        if ([jsonObject isKindOfClass:[NSDictionary class]]) {
            return jsonObject;
        }
    }
    @catch (NSException *exception) {
        NSLog(@"%@", exception);
    }

    return nil;
}


+ (NSDictionary *)jsonDictionaryForString:(NSString *)jsonString {
    if (![jsonString isKindOfClass:[NSString class]]) {
        return nil;
    }

    @try {
        NSString *aJsonString = [jsonString stringByReplacingOccurrencesOfString:@"\":null" withString:@"\":\"\""];

        NSData *jsonData = [aJsonString dataUsingEncoding:NSUTF8StringEncoding];
        if (!jsonData) {
            return nil;
        }

        NSError *error = nil;
        // iOS5中,苹果引入了一个解析JSON串的NSJSONSerialization类
        id jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:&error];
        if (!jsonObject || error) {
            NSLog(@"JSONSerialization Error:%@", error);
            return nil;
        }

        if ([jsonObject isKindOfClass:[NSDictionary class]]) {
            return jsonObject;
        }
    }
    @catch (NSException *exception) {
        NSLog(@"%@", exception);
    }

    return nil;
}

pragma mark - Data/String to Array


+ (NSArray *)jsonArrayForData:(NSData *)jsonData {
    if (![jsonData isKindOfClass:[NSData class]]) {
        return nil;
    }

    @try {
        NSError *error = nil;
        // iOS5中,苹果引入了一个解析JSON串的NSJSONSerialization类
        id jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:&error];
        if (!jsonObject || error) {
            NSLog(@"JSONSerialization Error:%@", error);
            return nil;
        }

        if ([jsonObject isKindOfClass:[NSArray class]]) {
            return jsonObject;
        }
    }
    @catch (NSException *exception) {
        NSLog(@"%@", exception);
    }

    return nil;
}


+ (NSArray *)jsonArrayForString:(NSString *)jsonString {
    if (![jsonString isKindOfClass:[NSString class]]) {
        return nil;
    }

    @try {
        NSString *aJsonString = [jsonString stringByReplacingOccurrencesOfString:@"\":null" withString:@"\":\"\""];

        NSData *jsonData = [aJsonString dataUsingEncoding:NSUTF8StringEncoding];
        if (!jsonData) {
            return nil;
        }

        NSError *error = nil;
        // iOS5中,苹果引入了一个解析JSON串的NSJSONSerialization类
        id jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:&error];
        if (!jsonObject || error) {
            NSLog(@"JSONSerialization Error:%@", error);
            return nil;
        }

        if ([jsonObject isKindOfClass:[NSArray class]]) {
            return jsonObject;
        }
    }
    @catch (NSException *exception) {
        NSLog(@"%@", exception);
    }

    return nil;
}

你可能感兴趣的:(Json与字典、数组之间的转换)