GitHub 地址:JSONDemo
前言
當我們在創(chuàng)建數(shù)據(jù)Model時候嬉愧,如網(wǎng)絡請求數(shù)據(jù)過于復雜這時候就需要花費大量時間來創(chuàng)建數(shù)據(jù)模型芽狗,為了方便快捷,于是就引入了如下工具來將Json轉(zhuǎn)換成Model文件佳吞。
我也已經(jīng)將此項目打包生成Dmg文件放在Res文件夾當中,如需要下載安裝即可
Dmg文件
可視化界面操作
將需要轉(zhuǎn)換的json字符串放在坐標框內(nèi)棉安,轉(zhuǎn)換即可
實現(xiàn)代碼
//
// ViewController.m
// KJJsonToModelDemo
//
// Created by 楊科軍 on 2019/10/12.
// Copyright ? 2019 楊科軍. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic,strong) NSMutableArray *temps;
@property (weak) IBOutlet NSButton *annotationButton;
@property (weak) IBOutlet NSTextField *jsonTextField;
@property (weak) IBOutlet NSTextField *annotationTextField;
@property (weak) IBOutlet NSTextField *githubLabel;
@property (unsafe_unretained) IBOutlet NSTextView *modelTextView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.temps = [NSMutableArray array];
// Do any additional setup after loading the view.
}
- (void)setRepresentedObject:(id)representedObject {
[super setRepresentedObject:representedObject];
// Update the view, if already loaded.
}
#pragma mark - json轉(zhuǎn)字典
- (NSDictionary *)getDictWithJsonString:(NSString *)jsonString{
jsonString = [[[[jsonString stringByReplacingOccurrencesOfString:@"\n" withString:@""]stringByReplacingOccurrencesOfString:@"\t" withString:@""]stringByReplacingOccurrencesOfString:@" " withString:@""]stringByReplacingOccurrencesOfString:@"\\" withString:@""];
/// 解析字典
jsonString = [jsonString stringByReplacingOccurrencesOfString:@";};" withString:@"};"];
jsonString = [jsonString stringByReplacingOccurrencesOfString:@";}" withString:@"}"];
jsonString = [jsonString stringByReplacingOccurrencesOfString:@";" withString:@","];
jsonString = [jsonString stringByReplacingOccurrencesOfString:@"=" withString:@":"];
jsonString = [jsonString stringByReplacingOccurrencesOfString:@"(" withString:@"["];
jsonString = [jsonString stringByReplacingOccurrencesOfString:@")" withString:@"]"];
NSString *regex = @"[,{].*?:";
NSError *error;
NSRegularExpression *regular = [NSRegularExpression regularExpressionWithPattern:regex options:NSRegularExpressionCaseInsensitive error:&error];
NSArray *matches = [regular matchesInString:jsonString options:0 range:NSMakeRange(0, jsonString.length)];
NSMutableString* jsonMuStr = [[NSMutableString alloc]initWithString:jsonString];
int i = 1;
for (NSTextCheckingResult *match in matches) {
NSRange range = [match range];
NSString *mStr = [jsonString substringWithRange:range];
if(![mStr containsString:@"\""]){
[jsonMuStr insertString:@"\"" atIndex:range.location + i];
i += 2;
[jsonMuStr insertString:@"\"" atIndex:range.location + i + range.length - 3];
}
}
///
jsonString = [self correctErrValueWithJsonString:jsonMuStr];
NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSError *err;
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&err];
if(err) {
[self errorHint];
return nil;
}
return dic;
}
- (NSString *)correctErrValueWithJsonString:(NSString *)jsonStr{
NSString *regex = @":.*?,";
NSError *error;
NSRegularExpression *regular = [NSRegularExpression regularExpressionWithPattern:regex options:NSRegularExpressionCaseInsensitive error:&error];
NSArray *matches = [regular matchesInString:jsonStr options:0 range:NSMakeRange(0, jsonStr.length)];
NSMutableString* jsonMuStr = [[NSMutableString alloc]initWithString:jsonStr];
int i = 1;
for (NSTextCheckingResult *match in matches) {
NSRange range = [match range];
NSString *mStr = [jsonStr substringWithRange:range];
if(mStr.length > 2)
mStr = [mStr substringWithRange:NSMakeRange(1, mStr.length - 2)];
if(![mStr containsString:@"\""] && ![self isPureNumber:mStr] && ![mStr containsString:@"}"] && ![mStr containsString:@"["] && ![mStr containsString:@"true"] && ![mStr containsString:@"false"]){
[jsonMuStr insertString:@"\"" atIndex:range.location + i];
i += 2;
[jsonMuStr insertString:@"\"" atIndex:range.location + i + range.length - 3];
}
}
return jsonMuStr;
}
- (BOOL)isPureNumber:(NSString*)string{
return [self isPureInt:string] || [self isPureFloat:string];
}
- (BOOL)isPureInt:(NSString*)string{
NSScanner* scan = [NSScanner scannerWithString:string];
int val;
return[scan scanInt:&val] && [scan isAtEnd];
}
- (BOOL)isPureFloat:(NSString*)string{
NSScanner* scan = [NSScanner scannerWithString:string];
float val;
return[scan scanFloat:&val] && [scan isAtEnd];
}
/// 錯誤提示
- (void)errorHint{
NSAlert *alert = [[NSAlert alloc]init];
[alert addButtonWithTitle:@"確定"];
alert.messageText = @"錯誤";
alert.informativeText = @"json字符串不合法";
[alert setAlertStyle:NSAlertStyleWarning];
[alert beginSheetModalForWindow:[self.view window] completionHandler:^(NSModalResponse returnCode) {
}];
}
/// 開始轉(zhuǎn)換
- (IBAction)thouchTransformButtonAction:(NSButton *)sender {
[self.temps removeAllObjects];
NSString *jsonStr = self.jsonTextField.stringValue;
if(!jsonStr.length){
[self errorHint];
return;
}
NSDictionary *resultDic = [self getDictWithJsonString:jsonStr];
[self recursionForResultWithObj:resultDic];
NSMutableArray *finalResultArr = [NSMutableArray array];
for (NSArray *subArr in self.temps) {
if(![finalResultArr containsObject:subArr]){
[finalResultArr addObject:subArr];
}
}
finalResultArr = (NSMutableArray *)[[finalResultArr reverseObjectEnumerator] allObjects];
NSString *outputString = @"";
/// 分割注釋
NSArray *annArr = [self.annotationTextField.stringValue componentsSeparatedByString:@"\n"];
NSInteger count = annArr.count;
for (NSArray *subArr in finalResultArr) {
outputString = [outputString stringByAppendingString:@"************分割線************\n"];
for (NSInteger i=0; i<subArr.count; i++) {
NSString *tempString;
NSString *string = subArr[i];
if(self.annotationButton.state == NSControlStateValueOn){
NSString *annStr = @"";
if (i<count) {
annStr = annArr[i];
}
tempString = [NSString stringWithFormat:@"http:/// %@\n%@",annStr,string];
}else{
tempString = string;
}
outputString = [outputString stringByAppendingString:tempString];
}
}
self.modelTextView.string = outputString;
}
#pragma mark - 遞歸解析
- (void)recursionForResultWithObj:(id)obj{
if([obj isKindOfClass:[NSArray class]]){
for (id subObj in obj) {
[self recursionForResultWithObj:subObj];
}
}else if([obj isKindOfClass:[NSDictionary class]]){
NSMutableArray *temp = [NSMutableArray array];
for (NSString *key in ((NSDictionary*)obj).allKeys){
id value = [obj objectForKey:key];
if([value isKindOfClass:[NSArray class]] || [value isKindOfClass:[NSDictionary class]]){
[temp addObject:[NSString stringWithFormat:@"@property (nonatomic,strong) %@ *%@;//\n",[[value superclass] class],key]];
[self recursionForResultWithObj:value];
}else{
if([value isKindOfClass:[NSString class]]){
[temp addObject:[NSString stringWithFormat:@"@property (nonatomic,copy) %@ *%@;//\n",[NSString class],key]];
}else if([value isKindOfClass:[NSNumber class]]){
[temp addObject:[NSString stringWithFormat:@"@property (nonatomic,strong) %@ *%@;//\n",[NSNumber class],key]];
}else{
[temp addObject:[NSString stringWithFormat:@"@property (nonatomic,strong) %@ *%@;//\n",@"id",key]];
}
}
[self.temps addObject:temp];
}
}
}
@end
關(guān)于將mac程序打包生成dmg文件
這篇文章有介紹:Mac 軟件打包生成dmg文件