Skip to content

Commit

Permalink
增加JSON字符串的自动解析功能
Browse files Browse the repository at this point in the history
服务器如果返回的是JSON字符串,不是JSON对象,有了MJExtension,也可以自动转换为模型了
  • Loading branch information
CoderMJLee committed Apr 17, 2015
1 parent da0109d commit 2da0fc3
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
/* Begin PBXBuildFile section */
2D1B71C11AE093F900C32DF3 /* MJProperty.m in Sources */ = {isa = PBXBuildFile; fileRef = 2D1B71C01AE093F900C32DF3 /* MJProperty.m */; };
2D1B71C41AE0940900C32DF3 /* NSObject+MJProperty.m in Sources */ = {isa = PBXBuildFile; fileRef = 2D1B71C31AE0940900C32DF3 /* NSObject+MJProperty.m */; };
2D1B71E11AE122B900C32DF3 /* NSString+MJExtension.m in Sources */ = {isa = PBXBuildFile; fileRef = 2D1B71E01AE122B900C32DF3 /* NSString+MJExtension.m */; };
2D3576231A7913F30056D6BF /* Bag.m in Sources */ = {isa = PBXBuildFile; fileRef = 2D3576221A7913F30056D6BF /* Bag.m */; };
2D6CAFF61A5AA0BB00E2BA5E /* Ad.m in Sources */ = {isa = PBXBuildFile; fileRef = 2D6CAFEF1A5AA0BB00E2BA5E /* Ad.m */; };
2D6CAFF71A5AA0BB00E2BA5E /* Status.m in Sources */ = {isa = PBXBuildFile; fileRef = 2D6CAFF11A5AA0BB00E2BA5E /* Status.m */; };
Expand Down Expand Up @@ -43,6 +44,8 @@
2D1B71C01AE093F900C32DF3 /* MJProperty.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MJProperty.m; sourceTree = "<group>"; };
2D1B71C21AE0940900C32DF3 /* NSObject+MJProperty.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSObject+MJProperty.h"; sourceTree = "<group>"; };
2D1B71C31AE0940900C32DF3 /* NSObject+MJProperty.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSObject+MJProperty.m"; sourceTree = "<group>"; };
2D1B71DF1AE122B900C32DF3 /* NSString+MJExtension.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSString+MJExtension.h"; sourceTree = "<group>"; };
2D1B71E01AE122B900C32DF3 /* NSString+MJExtension.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSString+MJExtension.m"; sourceTree = "<group>"; };
2D2ABF0B1A7127DC0016C673 /* main.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = main.h; sourceTree = "<group>"; };
2D3576211A7913F30056D6BF /* Bag.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Bag.h; sourceTree = "<group>"; };
2D3576221A7913F30056D6BF /* Bag.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Bag.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -169,6 +172,8 @@
2D9CAF47192FA60E0011F500 /* NSObject+MJCoding.m */,
2D9CAF48192FA60E0011F500 /* NSObject+MJKeyValue.h */,
2D9CAF49192FA60E0011F500 /* NSObject+MJKeyValue.m */,
2D1B71DF1AE122B900C32DF3 /* NSString+MJExtension.h */,
2D1B71E01AE122B900C32DF3 /* NSString+MJExtension.m */,
);
path = MJExtension;
sourceTree = "<group>";
Expand Down Expand Up @@ -232,6 +237,7 @@
2D6CAFF71A5AA0BB00E2BA5E /* Status.m in Sources */,
2D6CAFF81A5AA0BB00E2BA5E /* StatusResult.m in Sources */,
2D9CAF2E192FA6020011F500 /* main.m in Sources */,
2D1B71E11AE122B900C32DF3 /* NSString+MJExtension.m in Sources */,
2D9CAF59192FA60E0011F500 /* NSObject+MJKeyValue.m in Sources */,
2D9CAF58192FA60E0011F500 /* NSObject+MJCoding.m in Sources */,
2D3576231A7913F30056D6BF /* Bag.m in Sources */,
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#import "MJProperty.h"
#import "MJType.h"
#import "MJConst.h"
#import "NSString+MJExtension.h"
#import "MJFoundation.h"

@implementation NSObject (MJKeyValue)
Expand Down Expand Up @@ -76,6 +77,11 @@ - (instancetype)setKeyValues:(NSDictionary *)keyValues

- (instancetype)setKeyValues:(NSDictionary *)keyValues error:(NSError *__autoreleasing *)error
{
// 如果是JSON字符串
if ([keyValues isKindOfClass:[NSString class]]) {
keyValues = [((NSString *)keyValues) JSONObject];
}

MJAssertError([keyValues isKindOfClass:[NSDictionary class]], self, error, @"keyValues参数不是一个字典");

@try {
Expand All @@ -100,8 +106,12 @@ - (instancetype)setKeyValues:(NSDictionary *)keyValues error:(NSError *__autorel
// 2.如果是模型属性
MJType *type = property.type;
Class typeClass = type.typeClass;
Class objectClass = [property objectClassInArrayFromClass:[self class]];
if (!type.isFromFoundation && typeClass) {
value = [typeClass objectWithKeyValues:value];
} else if (objectClass) {
// 3.字典数组-->模型数组
value = [objectClass objectArrayWithKeyValuesArray:value];
} else if (typeClass == [NSString class]) {
if ([value isKindOfClass:[NSNumber class]]) {
// NSNumber -> NSString
Expand Down Expand Up @@ -129,12 +139,6 @@ - (instancetype)setKeyValues:(NSDictionary *)keyValues error:(NSError *__autorel
value = @([value intValue]);
}
}
} else {
Class objectClass = [property objectClassInArrayFromClass:[self class]];
if (objectClass) {
// 3.字典数组-->模型数组
value = [objectClass objectArrayWithKeyValuesArray:value];
}
}

// 4.赋值
Expand Down Expand Up @@ -170,6 +174,11 @@ + (NSArray *)objectArrayWithKeyValuesArray:(NSArray *)keyValuesArray

+ (NSArray *)objectArrayWithKeyValuesArray:(NSArray *)keyValuesArray error:(NSError *__autoreleasing *)error
{
// 如果是JSON字符串
if ([keyValuesArray isKindOfClass:[NSString class]]) {
keyValuesArray = [((NSString *)keyValuesArray) JSONObject];
}

// 1.判断真实性
MJAssertError([keyValuesArray isKindOfClass:[NSArray class]], nil, error, @"keyValuesArray参数不是一个数组");

Expand Down Expand Up @@ -238,16 +247,14 @@ - (NSDictionary *)keyValuesWithError:(NSError *__autoreleasing *)error
// 2.如果是模型属性
MJType *type = property.type;
Class typeClass = type.typeClass;
Class objectClass = [property objectClassInArrayFromClass:[self class]];
if (!type.isFromFoundation && typeClass) {
value = [value keyValues];
} else if (objectClass) {
// 3.处理数组里面有模型的情况
value = [objectClass keyValuesArrayWithObjectArray:value];
} else if (typeClass == [NSURL class]) {
value = [value absoluteString];
} else {
Class objectClass = [property objectClassInArrayFromClass:[self class]];
if (objectClass) {
// 3.处理数组里面有模型的情况
value = [objectClass keyValuesArrayWithObjectArray:value];
}
}

// 4.赋值
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// NSString+MJExtension.h
// TestTabBar
//
// Created by MJ Lee on 15/4/17.
// Copyright (c) 2015年 Mac Z. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface NSString (MJExtension)
- (id)JSONObject;
@end
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// NSString+MJExtension.m
// TestTabBar
//
// Created by MJ Lee on 15/4/17.
// Copyright (c) 2015年 Mac Z. All rights reserved.
//

#import "NSString+MJExtension.h"

@implementation NSString (MJExtension)
- (id)JSONObject
{
return [NSJSONSerialization JSONObjectWithData:[self dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:nil];
}
@end
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
## 能做什么?
* MJExtension是一套`字典和模型之间互相转换`的超轻量级框架
* MJExtension能完成的功能
* `字典(JSON)` --> `模型(Model)`
* `字典(JSON)` --> `模型(Model)`
* `JSON字符串` --> `模型(Model)`
* `模型(Model)` --> `字典(JSON)`
* `字典数组(JSON Array)` --> `模型数组(Model Array)`
* `字典数组(JSON Array)` --> `模型数组(Model Array)`
* `JSON字符串` --> `模型数组(Model Array)`
* `模型数组(Model Array)` --> `字典数组(JSON Array)`
* 详尽用法主要参考 main.m中的各个函数 以及 `NSObject+MJKeyValue.h`

Expand Down

0 comments on commit 2da0fc3

Please sign in to comment.