網絡篇-NSJSONSerialization轉JSON

jopen 8年前發布 | 14K 次閱讀 JSON iOS開發 移動開發

簡介

  • NSJSONSerialization

    介紹:
        在了解NSJSONSerialization之前我們需要知道JSON這個東西,JSON是什么呢!是一種輕量級的數據交換格式,更可以
        理解為后臺服務器傳來的奇怪數據,然而NSJSONSerialization就是可以解開這個奇怪數據的方法,其實解開的方法有
        很多,如:TouchJSON、SBJSON、JSONKit等等,但是NSJSONSerialization是蘋果自家開發,所以性能方面的話應該
        就不用說了,畢竟是親生的,但是用什么還是處決你們自己,,這里的話我主推NSJSONSerialization
    
    常用方法:
        1、json數據轉OC對象
            + (id)JSONObjectWithData:(NSData *)data options:(NSJSONReadingOptions)opt error:(NSError **)error;
        2、OC對象數據轉json
            + (NSData *)dataWithJSONObject:(id)obj options:(NSJSONWritingOptions)opt error:(NSError **)error;
  • 1、JSON轉NSDictionary

    /*
        JSON轉字典
    */
    -(void)jsonToDict{
        //創建URL對象
        NSURL *url = [NSURL URLWithString:@"http://192.168.1.0:8080/login?username=LitterL&pwd=123&type=JSON"];
        //創建請求
        NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url];
        //發送異步請求
        [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc]init] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
            /*
             第一個參數:要解析的二進制數據
             第二個參數:解析json的選項
                NSJSONReadingMutableContainers = (1UL << 0), 最外層是可變的字典和數組
                NSJSONReadingMutableLeaves = (1UL << 1),     里面的字符串也是可變的,iOS7
                NSJSONReadingAllowFragments = (1UL << 2)     最外層既不是字典也不是數組
                kNilOptions為什么都沒有
             第三個參數:錯誤信息
             */
            NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
    
            NSLog(@"字典為--------%@",dict);
            NSLog(@"字典中的success為-------------%@",dict[@"success"]);
        }];
    }
  • 2、NSDictionary轉JSON

    /*
        字典轉JSON
    */
    -(void)DictToJson{
        //1、創建一個NSDictionary
        NSDictionary *dict = @{
                    @"Name":@"LitterL",
                    @"Age":@"20"
                    };
        //2、判斷是否能轉為Json數據
        BOOL isValidJSONObject =  [NSJSONSerialization isValidJSONObject:dict];
        if (isValidJSONObject) {
            /*
             第一個參數:OC對象 也就是我們dict
             第二個參數:
                NSJSONWritingPrettyPrinted 排版
                kNilOptions 什么也不做
             */
            NSData *data =  [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:nil];
            //打印JSON數據
            NSLog(@"%@",[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]);
        }
    }

    這里的話是排版了的

    這里的話是沒有排版的,需更改:(大家自行對照)

    NSData *data = [NSJSONSerialization dataWithJSONObject:dict options:kNilOptions error:nil];

    補充:至于轉模型的話與轉字典差不多,所以的話這里我便不上代碼了,寫了這篇文章之后應該要等十多天才能在上碼了。因為要去出差了,不知道廣州天氣怎么樣,有碼友告知嗎

posted @ 2016-01-15 10:54 LitterL 閱讀( ... ) 評論( ... ) 編輯 收藏

來自: http://www.cnblogs.com/ljy-666/p/5132643.html

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!