MTJSONUtils 0.1.1

MTJSONUtils 0.1.1

测试已测试
语言语言 Obj-CObjective C
许可 BSD
发布上次发布2014年12月

Adam Kirk 维护。



  • Adam Kirk

一个 NSObject 分类,用于在处理从/to JSON 转换时使用。

安装

在您的 Podfile 中添加以下行

pod "MTJSONUtils"

方法

以下为从字典(或任何 NSObject)生成 JSON 数据的快捷方式

- (NSData *)JSONData;

NSJSONSerialization 的要求表明,所有对象必须是 NSString, NSNumber, NSArray, NSDictionary 或 NSNull 的实例。以下可以在任何 NSObject 上调用,并将返回接收器的安全深拷贝

- (id)objectWithJSONSafeObjects;

这将

  • 将 NSDate 对象替换为其 ISO 字符串表示形式。(例如:2012-08-12T04:17:30Z
  • 将任何其他非有效对象替换为其字符串描述。
  • 还将继续提供...

以下方法允许您查询包含数组(不仅仅是字典)的对象图

- (id)valueForComplexKeyPath:(NSString *)keyPath;
- (NSString *)stringValueForComplexKeyPath:(NSString *)key;

可用的索引值

  • 0..无穷大
  • first
  • last

示例

给定此对象

NSDictionary *dictionary = @{
    @"parent" : @{
        @"name" : @"Nathan",
        @"children" : @[ @{
            @"name" : @"adam",
            @"apple_products" : @[ @{
                @"title"        : @"macbook",
                @"price"        : @1399.99,
                @"date_bought"  : [NSDate dateWithTimeInterval:-22342 sinceDate:[NSDate date]]
            },
            @{
                @"title"        : @"mac mini",
                @"price"        : @599.99,
                @"date_bought"  : [NSDate dateWithTimeInterval:-30223 sinceDate:[NSDate date]]
            },
            @{
                @"title"        : @"iphone",
                @"price"        : @199.99,
                @"date_bought"  : [NSDate dateWithTimeInterval:-123453 sinceDate:[NSDate date]]
            }]
        },
        @{
            @"name" : @"amanda",
            @"apple_products" : @[ @{
                @"title"        : @"nano",
                @"price"        : @299.99,
                @"date_bought"  : [NSDate dateWithTimeInterval:-123453 sinceDate:[NSDate date]]
            }]
        },
        @{
            @"name" : @"andrew",
            @"apple_products" : @[ @{
                @"title"        : @"ipad",
                @"price"        : @499.99,
                @"date_bought"  : [NSDate dateWithTimeInterval:-2452342 sinceDate:[NSDate date]]
            },
            @{
                @"title"        : @"ipod touch",
                @"price"        : @399.99,
                @"date_bought"  : [NSDate dateWithTimeInterval:-430223 sinceDate:[NSDate date]]
            }]
        }]
    }
};

您可以像这样查询值

[[dictionary valueForComplexKeyPath:@"parent.name"]                                         // => "Nathan"
[[dictionary valueForComplexKeyPath:@"parent.children[0].name"]                             // => "adam"
[[dictionary valueForComplexKeyPath:@"parent.children[1].name"]                             // => "amanda"
[[dictionary valueForComplexKeyPath:@"parent.children[2].name"]                             // => "andrew"
[[dictionary valueForComplexKeyPath:@"parent.children[first].name"]                         // => "adam"
[[dictionary valueForComplexKeyPath:@"parent.children[last].name"]                          // => "andrew"
[[dictionary valueForComplexKeyPath:@"parent.children[first].apple_products[0].title"]      // => "macbook"
[[dictionary valueForComplexKeyPath:@"parent.children[first].apple_products[1].title"]      // => "mac mini"
[[dictionary valueForComplexKeyPath:@"parent.children[first].apple_products[2].title"]      // => "iphone"
[[dictionary valueForComplexKeyPath:@"parent.children[first].apple_products[last].title"]   // => "iphone"
[[dictionary valueForComplexKeyPath:@"parent.children[last].apple_products[last].title"]    // => "ipod touch"
[[dictionary valueForComplexKeyPath:@"parent.children[last].apple_products[last].price"]    // => 399.99

杂项

以及一些处理 NSNull 和 nil 的实用宏

// swaps NSNull for nil
#define NILL(a) ([a isKindOfClass:[NSNull class]] ? nil : a)
// swaps nil for NSNull
#define NUL(a) a ? a : [NSNull null]