iOS字符串转JSON



+(NSDictionary*)dictionaryWithJsonString:(NSString*)jsonString {
    
    if(jsonString ==nil) {
        
        return nil;
        
    }
    
    NSData*jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    
    NSError*err;
    
    NSDictionary*dic = [NSJSONSerialization JSONObjectWithData:jsonData
                        
                                                       options:NSJSONReadingMutableContainers
                        
                                                         error:&err];
    
    if(err) {
        
        NSLog(@"json解析失败:%@",err);
        
        return nil;
    }
    
    NSMutableDictionary*newdict=[[NSMutableDictionary alloc]init];
    
    for(NSString*keys in dic)
        
    {
        
        if(dic[keys]==[NSNull null])
            
        {
            
            [newdict setObject:@" "forKey:keys];
            
            continue;
            
        }
        
        [newdict setObject:[NSString stringWithFormat:@"%@",dic[keys]]forKey:keys];
        
    }
    

    
    return newdict;
    
}


你可能感兴趣的:(iOS字符串转JSON)