diff --git a/ESJsonFormat.xcodeproj/xcshareddata/xcschemes/ESJsonFormat.xcscheme b/ESJsonFormat.xcodeproj/xcshareddata/xcschemes/ESJsonFormat.xcscheme index 8b30244..a9adc8a 100755 --- a/ESJsonFormat.xcodeproj/xcshareddata/xcschemes/ESJsonFormat.xcscheme +++ b/ESJsonFormat.xcodeproj/xcshareddata/xcschemes/ESJsonFormat.xcscheme @@ -37,24 +37,32 @@ + shouldUseLaunchSchemeArgsEnv = "YES"> + + + + 0) { // 去掉长度为0的 + + BOOL haveSemicolon = [[obj substringFromIndex:[obj length] - 1] containsString:@","]; + if (haveSemicolon) { + obj = [obj stringByReplacingOccurrencesOfString:@"," withString:@""]; + } + + obj = [self restructuringString:obj]; + + if (haveSemicolon) { + obj = [obj stringByAppendingFormat:@"%@", @","]; + } + + } + + } + + [mutableArrayRowChars addObject:obj]; + }]; + + + + + //重组每行内容 在该有的位置添加分号 + NSMutableArray *mutableArrayRowChars2 = [NSMutableArray array]; + + [mutableArrayRowChars enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { + + //筛选正常的行 通过 ": [ { + if ([obj rangeOfString:@"\":"].location != NSNotFound && ![obj hasSuffix:@"["] && ![obj hasSuffix:@"{"]) { + + //遇到下一行是 } 直接追加 , + if ((idx + 1) < mutableArrayRowChars.count && ![mutableArrayRowChars[idx + 1] hasPrefix:@"}"]) { + obj = [obj stringByAppendingFormat:@"%@", @","]; + } + + } + + //末尾是} ] 时, 追加 , + else if ([obj hasSuffix:@"]"] || [obj hasSuffix:@"}"]){ + + //排除当前行是} 并且下一行是 ] 并且 不是最后一行的情况 + if ([obj hasSuffix:@"}"] && (idx + 1) < mutableArrayRowChars.count && [mutableArrayRowChars[idx + 1] hasSuffix:@"]"]) { + + }else { + obj = [obj stringByAppendingFormat:@"%@", @","]; + } + + } + + + + //移除最后一行 } 后面的 , + if (idx == mutableArrayRowChars.count - 1) { + obj = [obj stringByReplacingOccurrencesOfString:@"," withString:@""]; + } + + + [mutableArrayRowChars2 addObject:obj]; + }]; + + + + + + NSString *stringJSON = [mutableArrayRowChars2 componentsJoinedByString:@"\n"]; + + + NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:[stringJSON dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableLeaves error:NULL]; + + return dictionary; +} + + + +///**一级数字典*/ +//- (NSDictionary *)dictionaryWithString:(NSString *)string +//{ +// +// string = [string stringByReplacingOccurrencesOfString:@"{" withString:@""]; +// string = [string stringByReplacingOccurrencesOfString:@"}" withString:@""]; +// +// string = [string stringByReplacingOccurrencesOfString:@" " withString:@""]; +// string = [string stringByReplacingOccurrencesOfString:@"\"" withString:@""]; +// string = [string stringByReplacingOccurrencesOfString:@"\n" withString:@""]; +// +// +// NSArray *array = [string componentsSeparatedByString:@";"]; +// NSMutableArray *mutableArray = [NSMutableArray array]; +// +// [array enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { +// id key = [obj componentsSeparatedByString:@"="].firstObject; +// id value = [obj componentsSeparatedByString:@"="].lastObject; +// +// if ([self validateNum:value]) { +// +// //重新赋值, 避免前面是 一串0 +// if ([value rangeOfString:@"."].location != NSNotFound) { +// value = [@([value floatValue]) stringValue]; +// }else { +// value = [@([value integerValue]) stringValue]; +// } +// +// }else { +// value = [NSString stringWithFormat:@"%@%@%@", @"\"", @"=====", @"\""]; +// } +// +// if (![key isEqualToString:@""]) { +// key = [NSString stringWithFormat:@"%@%@%@", @"\"", key, @"\""]; +// [mutableArray addObject:[NSString stringWithFormat:@"%@:%@", key, value]]; +// } +// +// +// }]; +// +// +// NSString *stringJSON = [mutableArray componentsJoinedByString:@","]; +// stringJSON = [NSString stringWithFormat:@"{%@}", stringJSON]; +// +// NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:[stringJSON dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableLeaves error:NULL]; +// +// return dictionary; +//} + + + + + + + + + +/**验证整数*/ +- (BOOL)validateInteger:(NSString *)candidate; +{ + if ([candidate isEqualToString:@""]) { + return NO; } + NSString *regex = @"^-?[1-9]/d*$"; + + NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex]; + return [predicate evaluateWithObject:candidate]; +} + +/**验证浮点数*/ +- (BOOL)validateFloat:(NSString *)candidate; +{ + if ([candidate isEqualToString:@""]) { + return NO; + } + + NSString *regex = @"^(-?\\d+)(\\.\\d+)?$"; + + NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex]; + return [predicate evaluateWithObject:candidate]; +} + +/**重组字符串, 在字符串前后添加双引号*/ +- (NSString *)restructuringString:(NSString *)string; +{ + if ([self validateInteger:string] || [self validateFloat:string]) { + + //长度过长, 直接转为字符串 + if ([string componentsSeparatedByString:@"."].firstObject.length > 8) { + + string = [NSString stringWithFormat:@"%@%@%@", @"\"", @"字符串的值", @"\""]; + + } + else { + + //重新赋值, 避免前面是 一串0 + if ([string rangeOfString:@"."].location != NSNotFound) { + string = [@([string floatValue]) stringValue]; + + //如果转换后没有小数点 + if ([string rangeOfString:@"."].location == NSNotFound) { + string = [string stringByAppendingString:@".88"]; + } + + }else { + string = [@([string integerValue]) stringValue]; + } + } + + }else { + //重组value 前后加双引号 + string = [NSString stringWithFormat:@"%@%@%@", @"\"", @"字符串的值", @"\""]; + } + return string; } @end + diff --git a/ESJsonFormat/ESJsonFormat-Prefix.pch b/ESJsonFormat/ESJsonFormat-Prefix.pch index 00dc8ad..3a6b548 100644 --- a/ESJsonFormat/ESJsonFormat-Prefix.pch +++ b/ESJsonFormat/ESJsonFormat-Prefix.pch @@ -29,3 +29,14 @@ #define ESFormatResultNotification @"ESFormatResultNotification" #endif + + + + +/** + * 调试请在Scheme中配置, Excutable Xcode + */ + +#warning 不能用的, 在终端跑一下下面的命令, 把Xcode的UUID添加到插件中去 \ +\ +find ~/Library/Application\ Support/Developer/Shared/Xcode/Plug-ins -name Info.plist -maxdepth 3 | xargs -I{} defaults write {} DVTPlugInCompatibilityUUIDs -array-add `defaults read /Applications/Xcode.app/Contents/Info DVTPlugInCompatibilityUUID` \ No newline at end of file diff --git a/ESJsonFormat/Info.plist b/ESJsonFormat/Info.plist index 2e88279..38abf91 100644 --- a/ESJsonFormat/Info.plist +++ b/ESJsonFormat/Info.plist @@ -24,12 +24,14 @@ 1 DVTPlugInCompatibilityUUIDs + 0420B86A-AA43-4792-9ED0-6FE0F2B16A13 7FDF5C7A-131F-4ABB-9EDC-8C5F8F0B8A90 C4A681B0-4A26-480E-93EC-1218098B9AA0 AD68E85B-441B-4301-B564-A45E4919A6AD A16FF353-8441-459E-A50C-B071F53F51B7 9F75337B-21B4-4ADC-B558-F9CADF7073A7 E969541F-E6F9-4D25-8158-72DC3545A6C6 + 7265231C-39B4-402C-89E1-16167C4CC990 LSMinimumSystemVersion $(MACOSX_DEPLOYMENT_TARGET)