Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/visual-studio-code/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios NSRegularExpression捕获无效JSON的部分_Ios_Objective C_Regex - Fatal编程技术网

Ios NSRegularExpression捕获无效JSON的部分

Ios NSRegularExpression捕获无效JSON的部分,ios,objective-c,regex,Ios,Objective C,Regex,我试图对以下字符串执行正则表达式字符串提取:{“code”:5,“id”:104,“message”:“notworking”}。我需要执行提取,因为有时多个字符串可以像这样捆绑在一起:{…}{…}{…} 我已经在使用JSONSerialization来处理这样的情况:{“code”:5,“id”:104,“message”:“notworking”} 当我得到这样的消息时,我需要正则表达式来提取单个字符串:{“code”:5,“id”:104,“message”:“notworking”}{“

我试图对以下字符串执行正则表达式字符串提取:
{“code”:5,“id”:104,“message”:“notworking”}
。我需要执行提取,因为有时多个字符串可以像这样捆绑在一起:
{…}{…}{…}

我已经在使用JSONSerialization来处理这样的情况:
{“code”:5,“id”:104,“message”:“notworking”}

当我得到这样的消息时,我需要正则表达式来提取单个字符串:
{“code”:5,“id”:104,“message”:“notworking”}{“code”:5,“id”:101,“message”:“some message”}{“code”:5,“id”:105,“message”:“test”}

我有以下与字符串匹配的正则表达式:
{.*?“id”:104.*?}

NSError*error=NULL;
NSRegularExpression*regex=[NSRegularExpression regular expressionwithpattern:@“{.*?\“id\”:104.*?}”选项:nsregularexpressioncase不敏感错误:&error];
//regex正在此处返回(null)
NSRange range=[regex rangeOffirstMachinString:msg options:0 range:NSMakeRange(0[msg length]);
NSString*result=[msg substringWithRange:range];
//结果为空
[regex EnumerateMatchesInstalling:msg选项:0范围:NSMakeRange(0,[msg长度])使用块:^(NSTextCheckInResult*结果,NSMatchingFlags标志,BOOL*停止){
NSRange range=[结果范围索引:1];
NSString*regexStr=[msg substringWithRange:range];
}];
我相信问题在于我的正则表达式。我可能没有正确地逃脱它

我试过
{.*?\\\“id\\\”:104.*?}
,但结果是一样的

当多个字符串到达时,我需要一个正则表达式来管理和提取字符串,这样说对吗

你喜欢什么

 //{"code":5,"id":104,"message":"Not working"}

// initially convert your JSON string to NSData
NSData *objectData = [JsonString dataUsingEncoding:NSUTF8StringEncoding];
  NSError *error;
// deserialize using NSJSONSerialization
 NSDictionary *json = [NSJSONSerialization JSONObjectWithData:objectData options:0 error:&error];

// then serilize what ever your string
if (json)
{
NSString * code = json[@"code"];
NSString * ide = json[@"id"];
NSString * message = json[@"message"];
}
这是一个可能的解决方案,它不是很干净,但我必须使用正则表达式。您的问题可能更多地取决于WebSocket解析

想法:
•使用正则表达式隔离每个字典JSON。
•然后构造一个“字典数组JSON”(
bigJSONStr
,在本例中为
NSString

对于模式,请注意必须转义
{
}
,因为它们是在正则表达式中保留的。 我没有检查
NSError
参数,这当然是不推荐的

编辑:
附加说明/修改:而不是构造“bigJSONStr”(这很难看)


我认为这是一种JOSN格式,为什么不使用JSONERIATION来进行此操作?这是
{“code”:5,“id”:104,“message”:“Not working”}
字符串或措辞-看起来你在使用错误的工具进行此项工作。在我看来,这就像JSON。哪个正则表达式不是正确的做法考虑将字符串反序列化到字典(通过
JSONSerialization
)注意}和{在您的情况下需要转义,因为它们在正则表达式中有意义。不要只发布代码答案。提供一些您所做的事情以及原因。请更新。请在其他帖子中也保持类似。做得好。@Sleek-我的Bowlanks肯定支持代码,但与我的问题无关。我更新了我的描述。@Mark117-代码部分与您的问题相关,如果您添加一些额外的代码,我们将解决所有问题。我需要做的就是退出
{
}
。非常感谢您,Larme
  NSString *string = @"{\"code\":5,\"id\":104,\"message\":\"Not working\"}{\"code\":5,\"id\":101,\"message\":\"some message\"}{\"code\":5,\"id\":105,\"message\":\"test\"}";

  NSLog(@"String: %@", string);

  NSMutableArray *allSubDictAsStr = [[NSMutableArray alloc] init];

  NSString *pattern = @"\\{.*?\\}";
  NSError *errorRegex = nil;
  NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:&errorRegex];
  NSArray *results = [regex matchesInString:string options:0 range:NSMakeRange(0, [string length])];
  for (NSTextCheckingResult *aResult in results)
  {
    NSString *subJSONStr = [string substringWithRange:[aResult range]];
    [allSubDictAsStr addObject:subJSONStr];
  }
  NSString *bigJSONStr = [NSString stringWithFormat:@"[%@]", [allSubDictAsStr componentsJoinedByString:@","]];

  NSError *errorJSON = nil;
  NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:[bigJSONStr dataUsingEncoding:NSUTF8StringEncoding] options:0 error:&errorJSON];
  NSLog(@"JsonArray: %@", jsonArray);
NSMutableArray *allResponses = [[NSMutableArray alloc] init];
...
for (NSTextCheckingResult *aResult in results)
{
    NSString *subJSONStr = [string substringWithRange:[aResult range]];
    NSError *errorJSON = nil;
    NSDictionary *aResponseDict = [NSJSONSerialization JSONObjectWithData:[subJSONStr dataUsingEncoding:NSUTF8StringEncoding] options:0 error:&errorJSON];
    if (!errorJSON) [allResponses addObject:subJSONStr];
}
NSLog(@"allResponses: %@", allResponses);