【发布时间】:2012-09-20 16:27:44
【问题描述】:
我正在尝试从服务器解析 JSON,但是每当我使用 AFNetworking 时,解析器都不起作用,它会返回整个 JSON 而不是将其分离。
我使用 JSON 并注意到了这个 .NET 安全功能
{"d":[JSON INSIDE]}
JSON INSIDE = 包含 3 个数据对象的我的 JSON 数据,此处未显示。 导致默认 JSON Parser 仅将整个 JSON 返回为 1。 如果我删除 {"d":},它会正确解析为多个部分。但是,服务器返回的数据不能删除 d。 AFNetworking 中是否有任何地方可以告诉它忽略 d 部分并仅处理括号内的 [JSON]?
谢谢, 艾伦
更新:我在“d:”中获取数据后尝试再次重新解析,但我在 NSJSonSerialization 行上获得了 SigAbort。
NSString *innerData = [JSON objectForKey:@"d"];
NSLog(@"Inner Description %@", innerData);
NSError *jsonParsingError = nil;
NSDictionary *requestDictionary = [NSJSONSerialization JSONObjectWithData:[innerData dataUsingEncoding:NSUTF8StringEncoding] options:0 error:&jsonParsingError];
我正在解析的 JSON 示例:
{"d":[{"__type":"RequestSearchedInfo:#ChangeControlService.DataContracts","Description":"testing prod ","DueDate":"","Priority":{"__type":"ComponentInfo:#ChangeControlService.DataContracts","Id":2,"Name":"Medium"},"RequestId":368,"Requestor":{"__type":"ComponentInfo:#ChangeControlService.DataContracts","Id":5863,"Name":"A Person's Name"},"SentDate":"8\/31\/2012 4:28:11 PM","Status":{"__type":"StatusInfo:#ChangeControlService.DataContracts","Id":27,"IsEndState":false,"Name":"Pending Approval"},"System":{"__type":"ComponentInfo:#ChangeControlService.DataContracts","Id":11,"Name":"Internal-Testing"}},{"__type":"RequestSearchedInfo:#ChangeControlService.DataContracts","Description":"testing prod ","DueDate":"","Priority":{"__type":"ComponentInfo:#ChangeControlService.DataContracts","Id":2,"Name":"Medium"},"RequestId":367,"Requestor":{"__type":"ComponentInfo:#ChangeControlService.DataContracts","Id":5863,"Name":"A Persons Name"},"SentDate":"8\/31\/2012 4:27:40 PM","Status":{"__type":"StatusInfo:#ChangeControlService.DataContracts","Id":2,"IsEndState":false,"Name":"Pending Review"},"System":{"__type":"ComponentInfo:#ChangeControlService.DataContracts","Id":11,"Name":"Internal-Testing"}},{"__type":"RequestSearchedInfo:#ChangeControlService.DataContracts","Description":"testin","DueDate":"08\/03\/2012","Priority":{"__type":"ComponentInfo:#ChangeControlService.DataContracts","Id":3,"Name":"High"},"RequestId":29,"Requestor":{"__type":"ComponentInfo:#ChangeControlService.DataContracts","Id":5863,"Name":"A Persons Name"},"SentDate":"8\/2\/2012 1:58:34 PM","Status":{"__type":"StatusInfo:#ChangeControlService.DataContracts","Id":22,"IsEndState":false,"Name":"Acceptance Certification passed, Request to be Closed"},"System":{"__type":"ComponentInfo:#ChangeControlService.DataContracts","Id":11,"Name":"Internal-Testing"}}]}
谢谢!
【问题讨论】:
-
我不太清楚您遇到的输入和输出是什么。你的意思是上面的代码解析后返回
@{ @"d" : @1 }? -
如果存在封装 JSON 的 {"d": },代码将返回整个未解析的 JSON。如果我从 JSON 数据周围删除 {"d": },它将正确解析为单独的对象。所以说 JSON 返回中有 3 个对象。如果 d 存在,它会将所有 {"d":[JSON data]} 作为一个对象返回,而不是将它们作为 3 个对象返回 [JSON 数据对象 1]、[JSON 数据对象 2]。 [JSON 数据对象 3]。所以基本上,解析器根本不解析。它只是将调用中的所有数据作为一个对象返回。
-
有什么原因不能继续解析保存在“d”的对象内部的 json 吗?为什么不将对象存储为 innerObj 并继续解析呢?
-
所以它是一个字符串值
@{ @"d" : @"[JSON data object 1], [JSON data object 2]. [JSON data object 3]" }?我真的认为您需要使用真实的 JSON 发布示例。它不需要是您的实际数据,但我需要查看格式以便更好地了解正在发生的事情。 -
我发布了进一步的进展和我正在尝试解析的真实 JSON。因此,它将下面的整个内容作为 1 个对象返回,如果我删除 d 部分,它将把它们分成 3 个对象,因为那里有 3 个。
标签: objective-c ios json parsing afnetworking