zoukankan      html  css  js  c++  java
  • JSON解析(序列化和反序列化)

    JSON的序列化,代码示例:

        NSDictionary *dic = @{@"name":@"zhuzhu", @"age":@25};
        if (![NSJSONSerialization isValidJSONObject:dic]) {
            NSLog(@"该对象不支持转换");
            return;
        }
        /**
         - Top level object is an NSArray or NSDictionary
         - All objects are NSString, NSNumber, NSArray, NSDictionary, or NSNull
         - All dictionary keys are NSStrings
         - NSNumbers are not NaN or infinity
         */
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:kNilOptions error:nil];
        NSString *jsonStr = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
        NSLog(@"%@", jsonStr);

    JSON的反序列化,代码示例:

    NSDictionary *dic = @{@"name":@"zhuzhu", @"age":@25};
        if (![NSJSONSerialization isValidJSONObject:dic]) {
            NSLog(@"该对象不支持转换");
            return;
        }
        /**
         - Top level object is an NSArray or NSDictionary
         - All objects are NSString, NSNumber, NSArray, NSDictionary, or NSNull
         - All dictionary keys are NSStrings
         - NSNumbers are not NaN or infinity
         */
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:kNilOptions error:nil];
      //
    反序列的代码
    NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:nil]; NSLog(@"dic=%@", jsonDic);
  • 相关阅读:
    ReentrantLock与synchronized的差别
    读TIJ -1 对象入门
    wikioi 2573 大顶堆与小顶堆并用
    开源 免费 java CMS
    UVA10972
    springboot5
    spring-boot4
    spring-boot3
    spring-boot2
    spring-boot1
  • 原文地址:https://www.cnblogs.com/lyz0925/p/11578037.html
Copyright © 2011-2022 走看看