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);
  • 相关阅读:
    C#函数参数前的修饰符
    C#正则表达式简单案例解析
    C#正则表达式
    C#反射Reflection
    C#事件
    C#委托的使用
    无法解析主机报错
    SparkStreaming对接rabbitMQ
    load文件到hive,并保存
    kafka模式对比
  • 原文地址:https://www.cnblogs.com/lyz0925/p/11578037.html
Copyright © 2011-2022 走看看