<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                服務器返回給客戶端的數據,一般都是JSON格式或者XML格式(文件下載除外),JSON和XML的比較這里不詳述,可以參考這文章[http://www.cnblogs.com/SanMaoSpace/p/3139186.html](http://www.cnblogs.com/SanMaoSpace/p/3139186.html)?。 ?總的來說XML文件龐大,文件格式復雜,解析需要花費較多的資源和時間,傳輸占帶寬。JSON數據格式比較簡單,易于讀寫,格式都是壓縮的,占用帶寬小,移動開發首選。 JSON: 1、JSON的格式很像OC中的字典和數組 ~~~ {"name" : "jack", "age" : 10} {"names" : ["jack", "rose", "jim"]} ~~~ 標準JSON格式的注意點:key必須用雙引號 JSON – OC 轉換對照表: ![](https://box.kancloud.cn/2016-03-07_56dd4011a4953.jpg)![](https://box.kancloud.cn/2016-03-07_56dd4011bba03.jpg) 2、在iOS中,JSON的常見解析方案有4種 第三方框架:JSONKit、SBJson、TouchJSON(性能從左到右,越差)? 蘋果原生(自帶):NSJSONSerialization(性能最好) NSJSONSerialization的常見方法: ?2.1、JSON 轉 OC對象(其中的過程是json轉換為字典,字典再轉換為對象) `+ (id)JSONObjectWithData:(NSData *)data options:(NSJSONReadingOptions)opt error:(NSError **)error;` 例子: 請求服務器,返回json數據。json數據封裝到數組對象中。 服務器返回數據json格式: ![](https://box.kancloud.cn/2016-03-07_56dd4011e4009.jpg) ~~~ - (void)viewDidLoad { [super viewDidLoad]; NSURL *url = [NSURL URLWithString:@"http://localhost:8080/myService/video"]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) { if (connectionError || data==nil) { [MBProgressHUD showError:@"網絡繁忙,請稍后再試!"]; return ; } //json 轉化為data 得到字典 NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil]; //取出字典中的某一個key NSArray *videoArray = dict[@"videos"]; //字典轉模型 for (NSDictionary *videoDict in videoArray) { //字典轉模型(對象) Video *video = [Video videoWithDict:videoDict]; [self.videos addObject:video]; } }]; } ~~~ ?2.2、OC對象 轉JSON數據 (對象轉字典,字典再轉json) + (NSData *)dataWithJSONObject:(id)obj options:(NSJSONWritingOptions)opt error:(NSError **)error; ~~~ #import "ViewController.h" #import "Person.h" @interface ViewController () @property(nonatomic,strong) Person *person; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; _person = [[Person alloc]init]; _person.name = @"kobe"; _person.age = 24; _person.sex = @"男"; _person.phone = @"1112334444"; } -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ //person 轉字典 NSMutableDictionary *dict = [NSMutableDictionary dictionary]; dict[@"age"] = [NSString stringWithFormat:@"%d",self.person.age]; dict[@"name"] = self.person.name; dict[@"sex"] = self.person.sex; dict[@"phone"] = self.person.phone; NSData *data = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:nil]; NSString *dataStr = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]; NSLog(@"%@",dataStr); } @end ~~~ 結果: ~~~ ?{ ? "age" : "24", ? "sex" : "男", ? "phone" : "1112334444", ? "name" : "kobe" } ~~~ NSJSONReadingOptions枚舉: NSJSONReadingMutableContainers:返回可變容器,NSMutableDictionary或NSMutableArray。? NSJSONReadingMutableLeaves:返回的JSON對象中字符串的值為NSMutableString? NSJSONReadingAllowFragments:允許JSON字符串最外層既不是NSArray也不是NSDictionary,但必須是有效的JSON Fragment。 NSJSONWritingPrettyPrinted:的意思是將生成的json數據格式化輸出,這樣可讀性高,不設置則輸出的json字符串就是一整行。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看