yy_modelWithJSON & JSONModel

yy_modelWithJSON:json

Posted by kunnan on October 26, 2016

前言

  • 通过YYModel这个框架安全快速的完成JSON到模型的转换,其中还会介绍到一款好用的插件ESJsonFormat
  • JSONModel 这个框架,能够完成模型到JSON的转换

I、YYModel

1、1 首先创建模型类

创建模型类我们可以通过ESJsonFormat这款插件快速完成。

1.2、使用YYModel进行解析

II、JSONModel

2.1 @interface GACSocketModel : JSONModel

  • GACSocketModel.h
#import <JSONModel/JSONModel.h>

@interface GACSocketModel : JSONModel
@property (nonatomic, copy) NSString<Optional> *event;
@property (nonatomic, copy) NSDictionary<Optional> *data;
@property (nonatomic, copy) NSString<Optional> *type;
  • GACSocketModel.m
#import "GACSocketModel.h"

@implementation GACSocketModel

- (NSString *)socketModelToJSONString {
    NSAssert(self.data != nil, @"Argument must be non-nil");
    if (![self.data isKindOfClass:[NSDictionary class]]) {
        return nil;
    }
    NSString *bodyString = [self dictionnaryObjectToString:self.data];
    self.data = (NSDictionary *)bodyString;
    NSString *jsonString = [self toJSONString];
    jsonString = [jsonString stringByAppendingString:@"\r\n"];
    return jsonString;
}

- (NSString *)dictionnaryObjectToString:(NSDictionary *)object {
    NSError *error = nil;
    NSData *stringData =
    [NSJSONSerialization dataWithJSONObject:object options:NSJSONWritingPrettyPrinted error:&error];
    if (error) {
        return nil;
    }
    
    NSString *jsonString = [[NSString alloc] initWithData:stringData encoding:NSUTF8StringEncoding];
    // 字典对象用系统JSON序列化之后的data,转UTF-8后的jsonString里面会包含"\n"及" ",需要替换掉
    jsonString = [jsonString stringByReplacingOccurrencesOfString:@"\n" withString:@""];
    jsonString = [jsonString stringByReplacingOccurrencesOfString:@" " withString:@""];
    return jsonString;
}

2.2 效果

 发送中。。。data:{"type":"text","data":"{\"text\":\"我\"}","fromwxid":"","fromwxnick":"","event":"message"}

转载请注明: > yy_modelWithJSON & JSONModel