ios - RestKit POSTing multipartFormRequest 与字典参数会在数据中添加反斜杠

标签 ios objective-c json core-data restkit-0.20

我在使用 RestKit 发布数据时遇到问题。它实际上是在网络可用时同步数组DBMeetings对象(离线创建)。 DBMeetings 对象可以包含照片附件,因此我使用 RestKit 的以下方法。

multipartFormRequestWithObject:method:path:parameters:constructingBodyWithBlock:

问题是multipartFormRequest无法保留数据的JSON结构,这就是为什么我创建一个JSON字符串并将其作为参数发布,以便服务器端可以正确解析。我从以下SO的回答中发现了这个想法https://stackoverflow.com/a/19545138/1444321

NSString *jsonString = [self jsonFromArray:unSyncedMeetings];
NSMutableURLRequest *request = [[RKObjectManager sharedManager] multipartFormRequestWithObject:nil
                                                                                                method:RKRequestMethodPOST
                                                                                                  path:URL_MEETINGS_SYNC
                                                                                            parameters:@{@"total_files": [NSNumber numberWithInt:totalFiles], @"data": jsonString} constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
// just adding photo attachments in this block
}

如果我记录 jsonString 它会显示如下。

[{"id":92172,"total_topics":0,"optional_participants":[],"readonly_participants":[],"finish_time":"2014-11-06T15:23:00Z","tags":[],"title":"abc","sample":false,"start_time":"2014-11-06T14:23:00Z","organizer":{"name":"Mithooo Popla","id":42900,"user_id":0,"email":"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="cdb9a8beb9acaeaefdf4fd8dacafaee3aea2a0" rel="noreferrer noopener nofollow">[email protected]</a>"},"updated_at":"2014-11-06T14:25:34Z","meeting_topics":[{"id":800881,"title":"Tippih","meeting_id":92172,"type":"Topic","tags":[],"updated_at":"2014-11-06T14:25:34Z","created_at":"2014-11-06T14:23:47Z","children":[{"id":800882,"total_attachments":4,"topic_id":800881,"additional_owners":[],"title":"Tyhh\nVdh","tags":[],"type":"Note","attachments":[{"id":26291,"file_url":"http:\/\/api-test.meetingking.com\/uploads\/attachments\/26291-IMG_0371-b100db1da64b5c001bafb629b32bfffaee000ecb.PNG?1415283828","file_name":"IMG_0371.PNG"},{"id":26289,"file_url":"http:\/\/api-test.meetingking.com\/uploads\/attachments\/26289-IMG_0365-a7e2ee7f013b47d99a2ad06e95d7968676108f2b.PNG?1415283827","file_name":"IMG_0365.PNG"},{"id":26290,"file_url":"http:\/\/api-test.meetingking.com\/uploads\/attachments\/26290-IMG_0364-c5ae106dd58ec4c31b3e0fc82721da1cee0444b7.JPG?1415283827","file_name":"IMG_0364.JPG"},{"id":26292,"file_url":"http:\/\/api-test.meetingking.com\/uploads\/attachments\/26292-IMG_0366-0131a80b2199b6cc4d0d684f3acb78bc7a9c78a3.PNG?1415283828","file_name":"IMG_0366.PNG"}],"send_email":false,"updated_at":"2014-11-06T14:23:47Z","private":false,"created_at":"2014-11-06T14:23:47Z","user_id":42900,"deleted":false,"position":1,"unique_parent_id":"x-coredata:\/\/442A8ECF-97C6-493E-93D6-62BEBDEF5064\/DBTopics\/p2","status":0,"total_comments":0}],"presenter":{"user_id":0},"user_id":42900,"deleted":false,"subtopics":[],"parent_id":0,"position":1,"unique_id":"x-coredata:\/\/442A8ECF-97C6-493E-93D6-62BEBDEF5064\/DBTopics\/p2"}],"created_at":"2014-11-06T14:23:10Z","lock":false,"deleted":false,"required_participants":[]},{"id":91976,"total_topics":0,"optional_participants":[],"readonly_participants":[],"finish_time":"2014-11-22T22:04:00Z","location":"","finish_date":"2014-11-22T22:04:00Z","sample":false,"start_time":"2014-11-22T21:04:00Z","title":"aaaaaa 25 we'd future date. hggggg","updated_at":"2014-10-31T13:47:46Z","tags":[],"created_at":"2014-10-31T13:47:45Z","lock":false,"organizer":{"name":"mr  shake ","id":42902,"user_id":0,"email":"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6001170109130e18025320070d01090c4e030f0d" rel="noreferrer noopener nofollow">[email protected]</a>"},"meeting_topics":[],"deleted":false,"required_participants":[{"name":"mr  shake ","id":42902,"user_id":0,"email":"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="325345535b415c4a500172555f535b5e1c515d5f" rel="noreferrer noopener nofollow">[email protected]</a>"}],"start_date":"2014-11-22T21:04:00Z"},{"id":92187,"total_topics":0,"optional_participants":[],"readonly_participants":[],"finish_time":"2014-11-07T14:04:00Z","location":"","finish_date":"2014-11-07T14:04:00Z","sample":false,"start_time":"2014-11-07T13:04:00Z","title":"hhhhhhhhhh ggggjdfd,f hhh","updated_at":"2014-11-07T15:00:14Z","tags":[],"created_at":"2014-11-07T13:04:12Z","lock":false,"organizer":{"name":"mr  shake ","id":42902,"user_id":0,"email":"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="99f8eef8f0eaf7e1fbaad9fef4f8f0f5b7faf6f4" rel="noreferrer noopener nofollow">[email protected]</a>"},"meeting_topics":[],"deleted":false,"required_participants":[{"name":"mr  shake ","id":42902,"user_id":0,"email":"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2041574149534e58421360474d41494c0e434f4d" rel="noreferrer noopener nofollow">[email protected]</a>"}],"start_date":"2014-11-07T13:04:00Z"},{"id":92185,"total_topics":0,"optional_participants":[],"readonly_participants":[],"finish_time":"2014-11-07T12:49:37Z","location":"","finish_date":"2014-11-07T12:49:37Z","sample":false,"start_time":"2014-11-07T11:49:37Z","title":"jgfjfhtbn gggggggg","updated_at":"2014-11-07T11:49:37Z","tags":[],"created_at":"2014-11-07T11:49:37Z","lock":false,"organizer":{"name":"mr  shake ","id":42902,"user_id":0,"email":"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d8b9afb9b1abb6a0baeb98bfb5b9b1b4f6bbb7b5" rel="noreferrer noopener nofollow">[email protected]</a>"},"meeting_topics":[],"deleted":false,"required_participants":[{"name":"mr  shake ","id":42902,"user_id":0,"email":"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2b4a5c4a4258455349186b4c464a424705484446" rel="noreferrer noopener nofollow">[email protected]</a>"}],"start_date":"2014-11-07T11:49:37Z"}]

当我与 Charles 检查请求时,它仍然是正确的。这是查尔斯的屏幕截图。 enter image description here

但在服务器上,它的访问方式为

data="{\"meetings\":[{\"id\":91160,\"total_topics\":0,\"optional_participants\":[],\"readonly_participants\":[],\"finish_time\":\"2014-11-07T13:47:17Z\",\"location\":\"\",\"finish_date\":\"2014-11-07T13:47:17Z\",\"sample\":false,\"start_time\":\"2014-11-07T12:47:17Z\",\"title\":\"his 8898997998897897\",\"updated_at\":\"2014-11-07T12:47:27Z\",\"tags\":[],\"created_at\":\"2014-11-07T12:47:17Z\",\"lock\":false,\"organizer\":{\"name\":\"Rehmat Qadir\",\"id\":12891,\"user_id\":0,\"email\":\"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="daa8bfb2b7bbaef4b7b1b3b4bd9ab4a2acaef4b9b5b7" rel="noreferrer noopener nofollow">[email protected]</a>\"},\"meeting_topics\":[],\"deleted\":false,\"required_participants\":[{\"name\":\"Rehmat Qadir\",\"id\":12891,\"user_id\":0,\"email\":\"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8af8efe2e7ebfea4e7e1e3e4edcae4f2fcfea4e9e5e7" rel="noreferrer noopener nofollow">[email protected]</a>\"}],\"start_date\":\"2014-11-07T12:47:17Z\"}]}"

到处都添加了反斜杠。服务器端无法将data的值视为JSON对象,并且无法解析数据。 知道实际问题是什么吗?如有任何帮助,我们将不胜感激。

这就是我创建jsonString

的方式
+ (NSString *)jsonFromArray:(NSArray *)meetings {
    NSArray *requestDescriptors = [[RKObjectManager sharedManager] requestDescriptors];
    RKRequestDescriptor *requestDescriptorObject = [requestDescriptors objectAtIndex:10];

    NSMutableDictionary *jsonDictionary = [[NSMutableDictionary alloc] init];
    NSMutableArray *jsonArray = [[NSMutableArray alloc] init];

    if (meetings.count > 0) {
        for (DBMeetings *meeting in meetings) {
            NSDictionary *parametersForObject = [RKObjectParameterization parametersWithObject:meeting requestDescriptor:requestDescriptorObject error:nil];
            NSDictionary *meetingDictionary = [parametersForObject valueForKey:@"meetings"];
            [jsonArray addObject:meetingDictionary];
        }
    }

    [jsonDictionary setValue:jsonArray forKey:@"meetings"];
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonArray options:kNilOptions error:nil];

    NSString *jsonString;
    if(jsonData) {jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];}
    return jsonString;
}

最佳答案

您正在创建的 JSON 字符串看起来不错,发送它的方式和 Charles 日志也是如此。因此,这很可能是一个完全基于服务器的文本处理和内容转义问题。这通常应该是透明的,因此您需要展示如何处理它。最糟糕的情况是,您可以处理字符串并替换所有转义字符。

关于ios - RestKit POSTing multipartFormRequest 与字典参数会在数据中添加反斜杠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26806296/

相关文章:

iphone - 将 iPhone 应用程序转换为通用应用程序 : Grouped UITableView

objective-c - 构建 iOS 静态库

javascript - 从 JSON 中获取 Javascript 中不同键的多个集合

objective-c - 静态库 (ARC) 在非 ARC 应用程序上工作,遇到错误 : dyld: lazy symbol binding failed: Symbol not found: _objc_retainAutoreleasedReturnValue

ios - UINavigationController 中的 UISearchBar

ios - SKSpriteNode的 Sprite 只有在iPhone 7 Plus真机上模糊

javascript - 从 jquery 表单数据中删除 json 对象

iphone - IOS 7 - 如何从放置在 UITableViewCell 中的按钮获取 indexPath

ios - 使用 PPBadgeView 的自定义角标(Badge)未显示在导航项中 - swift

javascript - 无法访问 JSON 对象,因为 "wrapper"