From bd8d19b50706771430f7f0ef90376d699d0a30bf Mon Sep 17 00:00:00 2001 From: johnson Date: Thu, 2 Jun 2016 14:56:22 +0800 Subject: [PATCH 1/7] =?UTF-8?q?=E6=B7=BB=E5=8A=A0description=E6=8F=8F?= =?UTF-8?q?=E8=BF=B0=20=E8=BD=ACjson?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controller/ESInputJsonController.m | 254 +++++++++++++++++- 1 file changed, 249 insertions(+), 5 deletions(-) diff --git a/ESJsonFormat/Controller/ESInputJsonController.m b/ESJsonFormat/Controller/ESInputJsonController.m index 837893a..3bebc47 100644 --- a/ESJsonFormat/Controller/ESInputJsonController.m +++ b/ESJsonFormat/Controller/ESInputJsonController.m @@ -140,22 +140,266 @@ -(void)textDidChange:(NSNotification *)notification{ /** - * Determine whether a valid json + * 检查是否是一个有效的JSON */ -(id)dictionaryWithJsonStr:(NSString *)jsonString{ + + //如果是字典description的一部分, 直接截获 + id dicOrArray = [self dictionaryWithString:jsonString]; + if (dicOrArray) { + return dicOrArray; + } + + + //以JSON字符串的解析 jsonString = [[jsonString stringByReplacingOccurrencesOfString:@" " withString:@""] stringByReplacingOccurrencesOfString:@" " withString:@""]; NSLog(@"jsonString=%@",jsonString); NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding]; NSError *err; - NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData - options:NSJSONReadingMutableContainers - error:&err]; + dicOrArray = [NSJSONSerialization JSONObjectWithData:jsonData + options:NSJSONReadingMutableContainers + error:&err]; if (err) { return err; }else{ - return dic; + return dicOrArray; } } + + + + + + +/**多级字典*/ +- (NSDictionary *)dictionaryWithString:(NSString *)string +{ + NSArray *arrayData = [string componentsSeparatedByString:@"="]; + + //最原始的方法检查. + if (!arrayData.firstObject || !arrayData.lastObject) { + return nil; + } + + + + string = [string stringByReplacingOccurrencesOfString:@"(" withString:@"["]; + string = [string stringByReplacingOccurrencesOfString:@")" withString:@"]"]; + + string = [string stringByReplacingOccurrencesOfString:@" " withString:@""]; + string = [string stringByReplacingOccurrencesOfString:@"\"" withString:@""]; + + + NSString *firstChar = [string substringToIndex:1]; + NSString *lastChar = [string substringFromIndex:string.length - 1]; + + + //如果没有字典符号, 补上 + if (![firstChar isEqualToString:@"{"]) { + string = [NSString stringWithFormat:@"{\n%@", string]; + } + + if (![lastChar isEqualToString:@"}"]) { + string = [NSString stringWithFormat:@"%@\n}", string]; + } + + + + NSArray *array = [string componentsSeparatedByString:@"\n"]; + + + //重组每行内容 + NSMutableArray *mutableArrayRowChars = [NSMutableArray array]; + [array enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { + + //去除每一行中的 ; + obj = [obj stringByReplacingOccurrencesOfString:@";" withString:@""]; + + //筛选键值行 带等号的 + if ([obj rangeOfString:@"="].location != NSNotFound) { + + //重组key 前后加爽引号 + NSString *key = [obj componentsSeparatedByString:@"="].firstObject; + key = [NSString stringWithFormat:@"%@%@%@", @"\"", key, @"\""]; + + + NSString *value = [obj componentsSeparatedByString:@"="].lastObject; + + //筛出字典 数组 + BOOL isValue = !([value hasSuffix:@"{"] || [value hasSuffix:@"["]); + if (isValue) { + + //重新组合字典key + value = [self restructuringString:value]; + } + + //重组键值 + obj = [NSString stringWithFormat:@"%@:%@", key, value]; + } + //筛选数组行, 仅是字符串或数字的 + else if (![obj containsString:@"["] && ![obj containsString:@"]"] && ![obj containsString:@"{"] && ![obj containsString:@"}"]) { + + //重新组合 数组值 记录是否包含分号, 如果有, 先移除再补上 + 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)validateNum:(NSString *)candidate; +{ + if ([candidate isEqualToString:@""]) { + return NO; + } + + NSString *regex = @"^[0-9]+(.[0-9]{1,2})?$"; + + NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex]; + return [predicate evaluateWithObject:candidate]; +} + +/**重组字符串, 在字符串前后添加双引号*/ +- (NSString *)restructuringString:(NSString *)string; +{ + if ([self validateNum:string]) { + + //重新赋值, 避免前面是 一串0 + if ([string rangeOfString:@"."].location != NSNotFound) { + string = [@([string floatValue]) stringValue]; + }else { + string = [@([string integerValue]) stringValue]; + } + + }else { + //重组value 前后加双引号 + string = [NSString stringWithFormat:@"%@%@%@", @"\"", @"字符串的值", @"\""]; + } + return string; +} + @end From 124b747ed3859d281e7aee8b08a4fa1c0513f318 Mon Sep 17 00:00:00 2001 From: Johnson Date: Thu, 2 Jun 2016 23:47:33 +0800 Subject: [PATCH 2/7] =?UTF-8?q?=E6=B7=BB=E5=8A=A07.1.1=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ESJsonFormat/Info.plist | 1 + 1 file changed, 1 insertion(+) diff --git a/ESJsonFormat/Info.plist b/ESJsonFormat/Info.plist index 2e88279..3fbf22c 100644 --- a/ESJsonFormat/Info.plist +++ b/ESJsonFormat/Info.plist @@ -30,6 +30,7 @@ A16FF353-8441-459E-A50C-B071F53F51B7 9F75337B-21B4-4ADC-B558-F9CADF7073A7 E969541F-E6F9-4D25-8158-72DC3545A6C6 + 7265231C-39B4-402C-89E1-16167C4CC990 LSMinimumSystemVersion $(MACOSX_DEPLOYMENT_TARGET) From 8e492844511fcba06c7724a9ded4ae55c21c64ee Mon Sep 17 00:00:00 2001 From: Johnson Date: Fri, 3 Jun 2016 00:47:51 +0800 Subject: [PATCH 3/7] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=BA=A2=E8=89=B2?= =?UTF-8?q?=E5=AD=97=E4=BD=93,=20=20=E5=8E=BB=E9=99=A4=E4=B8=8B=E6=A0=87?= =?UTF-8?q?=E8=B6=8A=E7=95=8C=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../xcschemes/ESJsonFormat.xcscheme | 11 +++-- .../Controller/ESInputJsonController.m | 47 +++++++++++++------ 2 files changed, 40 insertions(+), 18 deletions(-) diff --git a/ESJsonFormat.xcodeproj/xcshareddata/xcschemes/ESJsonFormat.xcscheme b/ESJsonFormat.xcodeproj/xcshareddata/xcschemes/ESJsonFormat.xcscheme index 8b30244..ee4fc72 100755 --- a/ESJsonFormat.xcodeproj/xcshareddata/xcschemes/ESJsonFormat.xcscheme +++ b/ESJsonFormat.xcodeproj/xcshareddata/xcschemes/ESJsonFormat.xcscheme @@ -37,22 +37,25 @@ + shouldUseLaunchSchemeArgsEnv = "YES"> + + @@ -68,10 +71,10 @@ 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:@"%@", @","]; + } + } } From 5096dcf4fedc553c23f70cb0690297fbdcfcc973 Mon Sep 17 00:00:00 2001 From: johnson Date: Fri, 3 Jun 2016 13:58:41 +0800 Subject: [PATCH 4/7] =?UTF-8?q?=E6=B7=BB=E5=8A=A0UUID=E5=91=BD=E4=BB=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../xcshareddata/xcschemes/ESJsonFormat.xcscheme | 7 ++++++- ESJsonFormat/ESJsonFormat-Prefix.pch | 6 ++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/ESJsonFormat.xcodeproj/xcshareddata/xcschemes/ESJsonFormat.xcscheme b/ESJsonFormat.xcodeproj/xcshareddata/xcschemes/ESJsonFormat.xcscheme index ee4fc72..118b60e 100755 --- a/ESJsonFormat.xcodeproj/xcshareddata/xcschemes/ESJsonFormat.xcscheme +++ b/ESJsonFormat.xcodeproj/xcshareddata/xcschemes/ESJsonFormat.xcscheme @@ -47,7 +47,7 @@ + + Date: Sat, 4 Jun 2016 19:02:20 +0800 Subject: [PATCH 5/7] =?UTF-8?q?=E6=B2=A1=E5=BE=97=E4=BB=80=E4=B9=88?= =?UTF-8?q?=E5=86=85=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../xcshareddata/xcschemes/ESJsonFormat.xcscheme | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ESJsonFormat.xcodeproj/xcshareddata/xcschemes/ESJsonFormat.xcscheme b/ESJsonFormat.xcodeproj/xcshareddata/xcschemes/ESJsonFormat.xcscheme index 118b60e..e163167 100755 --- a/ESJsonFormat.xcodeproj/xcshareddata/xcschemes/ESJsonFormat.xcscheme +++ b/ESJsonFormat.xcodeproj/xcshareddata/xcschemes/ESJsonFormat.xcscheme @@ -47,7 +47,7 @@ + FilePath = "/Applications/Xcode7.1.1.app"> Date: Wed, 8 Jun 2016 11:22:38 +0800 Subject: [PATCH 6/7] xx --- .../xcshareddata/xcschemes/ESJsonFormat.xcscheme | 2 +- ESJsonFormat/Controller/ESInputJsonController.m | 3 +-- ESJsonFormat/ESJsonFormat-Prefix.pch | 5 +++++ ESJsonFormat/Info.plist | 1 + 4 files changed, 8 insertions(+), 3 deletions(-) diff --git a/ESJsonFormat.xcodeproj/xcshareddata/xcschemes/ESJsonFormat.xcscheme b/ESJsonFormat.xcodeproj/xcshareddata/xcschemes/ESJsonFormat.xcscheme index 118b60e..a9adc8a 100755 --- a/ESJsonFormat.xcodeproj/xcshareddata/xcschemes/ESJsonFormat.xcscheme +++ b/ESJsonFormat.xcodeproj/xcshareddata/xcschemes/ESJsonFormat.xcscheme @@ -47,7 +47,7 @@ 1 DVTPlugInCompatibilityUUIDs + 0420B86A-AA43-4792-9ED0-6FE0F2B16A13 7FDF5C7A-131F-4ABB-9EDC-8C5F8F0B8A90 C4A681B0-4A26-480E-93EC-1218098B9AA0 AD68E85B-441B-4301-B564-A45E4919A6AD From 329fe07c42e1dfa7276cb5221fc5085487f97cbb Mon Sep 17 00:00:00 2001 From: johnson Date: Wed, 8 Jun 2016 13:46:27 +0800 Subject: [PATCH 7/7] =?UTF-8?q?=E4=BF=AE=E6=94=B9float=E6=8F=90=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../xcschemes/ESJsonFormat.xcscheme | 2 +- .../Controller/ESInputJsonController.m | 47 +++++++++++++++---- 2 files changed, 39 insertions(+), 10 deletions(-) diff --git a/ESJsonFormat.xcodeproj/xcshareddata/xcschemes/ESJsonFormat.xcscheme b/ESJsonFormat.xcodeproj/xcshareddata/xcschemes/ESJsonFormat.xcscheme index e163167..a9adc8a 100755 --- a/ESJsonFormat.xcodeproj/xcshareddata/xcschemes/ESJsonFormat.xcscheme +++ b/ESJsonFormat.xcodeproj/xcshareddata/xcschemes/ESJsonFormat.xcscheme @@ -61,7 +61,7 @@ + FilePath = "/Applications/Xcode.app"> 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 { @@ -421,3 +449,4 @@ - (NSString *)restructuringString:(NSString *)string; } @end +