【转】H5收不到iOS传递的JSON的解决方案

经过分析失败的原因是

  • 因为JSON字符串带空格换行符,空格缘故
/**
 *  JSON字符串转NSDictionary
 *
 *  @param jsonString JSON字符串
 *
 *  @return NSDictionary
 */
+ (NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString {
    if (jsonString == nil) {
        return nil;
    }
    NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    NSError *error;
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
    if(error) {
        NSLog(@"json解析失败:%@",error);
        return nil;
    }
    return dic;
}
/**
 *  字典转JSON字符串
 *
 *  @param dic 字典
 *
 *  @return JSON字符串
 */
+ (NSString*) dictionaryToJson:(NSDictionary *)dic{
    NSError *parseError = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&parseError];
    NSString *jsonString =  [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
   
    NSMutableString *mutStr = [NSMutableString stringWithString:jsonString];
    
    NSRange range = {0,jsonString.length};
    
    //去掉字符串中的空格
    
    [mutStr replaceOccurrencesOfString:@" " withString:@"" options:NSLiteralSearch range:range];
    
    NSRange range2 = {0,mutStr.length};
    
    //* 去掉字符串中的换行符 否则H5收不到 
    
    [mutStr replaceOccurrencesOfString:@"\n" withString:@"" options:NSLiteralSearch range:range2];
    
    return mutStr;
}
复制代码

题外话

  • iOS11之后生成的jSON默认去掉空格和换行符

    • option NSJSONWritingSortedKeys
  if (@available(iOS 11.0, *))
  {
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:object
                                                           options:NSJSONWritingSortedKeys // Pass 0 if you don't care about the readability of the generated string
                                                             error:&error];
  } 
复制代码
  • iOS11之前生成的jSON默认带空格和换行符需要手动去掉
  NSData *jsonData = [NSJSONSerialization dataWithJSONObject:object
                                                           options:NSJSONWritingPrettyPrinted // Pass 0 if you don't care about the readability of the generated string
                                                             error:&error];
复制代码
  • 去掉空格和换行符
    NSMutableString *mutStr = [NSMutableString stringWithString:jsonString];
    
    NSRange range = {0,jsonString.length};
    
    //去掉字符串中的空格
    
    [mutStr replaceOccurrencesOfString:@" " withString:@"" options:NSLiteralSearch range:range];
    
    NSRange range2 = {0,mutStr.length};
    
    //去掉字符串中的换行符
    
    [mutStr replaceOccurrencesOfString:@"\n" withString:@"" options:NSLiteralSearch range:range2];

复制代码

移动端传给html 的json

- (void)executeJsString:(NSString *)jsString JsonString:(NSString *)jsonString {
    jsString = [NSString stringWithFormat:@"%@(%@)", jsString, jsonString];//不要加 ' '
    [self.webView stringByEvaluatingJavaScriptFromString:jsString];
}


转自:https://juejin.im/post/5ab71b306fb9a028d7009007
来源:掘金

posted @ 2020-05-22 17:18  超级大熊  阅读(612)  评论(0编辑  收藏  举报