菜鸟教程小白 发表于 2022-12-12 12:34:26

ios - RestKit 使用字典参数发布 multipartFormRequest 在数据中添加反斜杠


                                            <p><p>我在使用 RestKit 发布数据时遇到问题。当网络可用时,它实际上是在同步 Array <code>DBMeetings</code> 对象(离线创建)。 <code>DBMeetings</code> 对象可以有照片附件,所以我使用的是 RestKit 的以下方法。</p>

<pre><code>multipartFormRequestWithObject:method:path:parameters:constructingBodyWithBlock:
</code></pre>

<p>问题是 <code>multipartFormRequest</code> 不能保留数据的 JSON 结构,这就是为什么我要创建一个 JSON 字符串并将其作为参数发布,以便服务器端可以适本地解析。我从遵循 SO 的回答 <a href="https://stackoverflow.com/a/19545138/1444321" rel="noreferrer noopener nofollow">https://stackoverflow.com/a/19545138/1444321</a> 中发现了这个想法</p>

<pre><code>NSString *jsonString = ;
NSMutableURLRequest *request = [ multipartFormRequestWithObject:nil
                                                                                                method:RKRequestMethodPOST
                                                                                                path:URL_MEETINGS_SYNC
                                                                                          parameters:@{@&#34;total_files&#34;: , @&#34;data&#34;: jsonString} constructingBodyWithBlock:^(id&lt;AFMultipartFormData&gt; formData) {
// just adding photo attachments in this block
}
</code></pre>

<p>如果我记录 <code>jsonString</code>,它会像这样显示。</p>

<pre><code>[{&#34;id&#34;:92172,&#34;total_topics&#34;:0,&#34;optional_participants&#34;:[],&#34;readonly_participants&#34;:[],&#34;finish_time&#34;:&#34;2014-11-06T15:23:00Z&#34;,&#34;tags&#34;:[],&#34;title&#34;:&#34;abc&#34;,&#34;sample&#34;:false,&#34;start_time&#34;:&#34;2014-11-06T14:23:00Z&#34;,&#34;organizer&#34;:{&#34;name&#34;:&#34;Mithooo Popla&#34;,&#34;id&#34;:42900,&#34;user_id&#34;:0,&#34;email&#34;:&#34;[email protected]&#34;},&#34;updated_at&#34;:&#34;2014-11-06T14:25:34Z&#34;,&#34;meeting_topics&#34;:[{&#34;id&#34;:800881,&#34;title&#34;:&#34;Tippih&#34;,&#34;meeting_id&#34;:92172,&#34;type&#34;:&#34;Topic&#34;,&#34;tags&#34;:[],&#34;updated_at&#34;:&#34;2014-11-06T14:25:34Z&#34;,&#34;created_at&#34;:&#34;2014-11-06T14:23:47Z&#34;,&#34;children&#34;:[{&#34;id&#34;:800882,&#34;total_attachments&#34;:4,&#34;topic_id&#34;:800881,&#34;additional_owners&#34;:[],&#34;title&#34;:&#34;Tyhh\nVdh&#34;,&#34;tags&#34;:[],&#34;type&#34;:&#34;Note&#34;,&#34;attachments&#34;:[{&#34;id&#34;:26291,&#34;file_url&#34;:&#34;http:\/\/api-test.meetingking.com\/uploads\/attachments\/26291-IMG_0371-b100db1da64b5c001bafb629b32bfffaee000ecb.PNG?1415283828&#34;,&#34;file_name&#34;:&#34;IMG_0371.PNG&#34;},{&#34;id&#34;:26289,&#34;file_url&#34;:&#34;http:\/\/api-test.meetingking.com\/uploads\/attachments\/26289-IMG_0365-a7e2ee7f013b47d99a2ad06e95d7968676108f2b.PNG?1415283827&#34;,&#34;file_name&#34;:&#34;IMG_0365.PNG&#34;},{&#34;id&#34;:26290,&#34;file_url&#34;:&#34;http:\/\/api-test.meetingking.com\/uploads\/attachments\/26290-IMG_0364-c5ae106dd58ec4c31b3e0fc82721da1cee0444b7.JPG?1415283827&#34;,&#34;file_name&#34;:&#34;IMG_0364.JPG&#34;},{&#34;id&#34;:26292,&#34;file_url&#34;:&#34;http:\/\/api-test.meetingking.com\/uploads\/attachments\/26292-IMG_0366-0131a80b2199b6cc4d0d684f3acb78bc7a9c78a3.PNG?1415283828&#34;,&#34;file_name&#34;:&#34;IMG_0366.PNG&#34;}],&#34;send_email&#34;:false,&#34;updated_at&#34;:&#34;2014-11-06T14:23:47Z&#34;,&#34;private&#34;:false,&#34;created_at&#34;:&#34;2014-11-06T14:23:47Z&#34;,&#34;user_id&#34;:42900,&#34;deleted&#34;:false,&#34;position&#34;:1,&#34;unique_parent_id&#34;:&#34;x-coredata:\/\/442A8ECF-97C6-493E-93D6-62BEBDEF5064\/DBTopics\/p2&#34;,&#34;status&#34;:0,&#34;total_comments&#34;:0}],&#34;presenter&#34;:{&#34;user_id&#34;:0},&#34;user_id&#34;:42900,&#34;deleted&#34;:false,&#34;subtopics&#34;:[],&#34;parent_id&#34;:0,&#34;position&#34;:1,&#34;unique_id&#34;:&#34;x-coredata:\/\/442A8ECF-97C6-493E-93D6-62BEBDEF5064\/DBTopics\/p2&#34;}],&#34;created_at&#34;:&#34;2014-11-06T14:23:10Z&#34;,&#34;lock&#34;:false,&#34;deleted&#34;:false,&#34;required_participants&#34;:[]},{&#34;id&#34;:91976,&#34;total_topics&#34;:0,&#34;optional_participants&#34;:[],&#34;readonly_participants&#34;:[],&#34;finish_time&#34;:&#34;2014-11-22T22:04:00Z&#34;,&#34;location&#34;:&#34;&#34;,&#34;finish_date&#34;:&#34;2014-11-22T22:04:00Z&#34;,&#34;sample&#34;:false,&#34;start_time&#34;:&#34;2014-11-22T21:04:00Z&#34;,&#34;title&#34;:&#34;aaaaaa 25 we&#39;d future date. hggggg&#34;,&#34;updated_at&#34;:&#34;2014-10-31T13:47:46Z&#34;,&#34;tags&#34;:[],&#34;created_at&#34;:&#34;2014-10-31T13:47:45Z&#34;,&#34;lock&#34;:false,&#34;organizer&#34;:{&#34;name&#34;:&#34;mrshake &#34;,&#34;id&#34;:42902,&#34;user_id&#34;:0,&#34;email&#34;:&#34;[email protected]&#34;},&#34;meeting_topics&#34;:[],&#34;deleted&#34;:false,&#34;required_participants&#34;:[{&#34;name&#34;:&#34;mrshake &#34;,&#34;id&#34;:42902,&#34;user_id&#34;:0,&#34;email&#34;:&#34;[email protected]&#34;}],&#34;start_date&#34;:&#34;2014-11-22T21:04:00Z&#34;},{&#34;id&#34;:92187,&#34;total_topics&#34;:0,&#34;optional_participants&#34;:[],&#34;readonly_participants&#34;:[],&#34;finish_time&#34;:&#34;2014-11-07T14:04:00Z&#34;,&#34;location&#34;:&#34;&#34;,&#34;finish_date&#34;:&#34;2014-11-07T14:04:00Z&#34;,&#34;sample&#34;:false,&#34;start_time&#34;:&#34;2014-11-07T13:04:00Z&#34;,&#34;title&#34;:&#34;hhhhhhhhhh ggggjdfd,f hhh&#34;,&#34;updated_at&#34;:&#34;2014-11-07T15:00:14Z&#34;,&#34;tags&#34;:[],&#34;created_at&#34;:&#34;2014-11-07T13:04:12Z&#34;,&#34;lock&#34;:false,&#34;organizer&#34;:{&#34;name&#34;:&#34;mrshake &#34;,&#34;id&#34;:42902,&#34;user_id&#34;:0,&#34;email&#34;:&#34;[email protected]&#34;},&#34;meeting_topics&#34;:[],&#34;deleted&#34;:false,&#34;required_participants&#34;:[{&#34;name&#34;:&#34;mrshake &#34;,&#34;id&#34;:42902,&#34;user_id&#34;:0,&#34;email&#34;:&#34;[email protected]&#34;}],&#34;start_date&#34;:&#34;2014-11-07T13:04:00Z&#34;},{&#34;id&#34;:92185,&#34;total_topics&#34;:0,&#34;optional_participants&#34;:[],&#34;readonly_participants&#34;:[],&#34;finish_time&#34;:&#34;2014-11-07T12:49:37Z&#34;,&#34;location&#34;:&#34;&#34;,&#34;finish_date&#34;:&#34;2014-11-07T12:49:37Z&#34;,&#34;sample&#34;:false,&#34;start_time&#34;:&#34;2014-11-07T11:49:37Z&#34;,&#34;title&#34;:&#34;jgfjfhtbn gggggggg&#34;,&#34;updated_at&#34;:&#34;2014-11-07T11:49:37Z&#34;,&#34;tags&#34;:[],&#34;created_at&#34;:&#34;2014-11-07T11:49:37Z&#34;,&#34;lock&#34;:false,&#34;organizer&#34;:{&#34;name&#34;:&#34;mrshake &#34;,&#34;id&#34;:42902,&#34;user_id&#34;:0,&#34;email&#34;:&#34;[email protected]&#34;},&#34;meeting_topics&#34;:[],&#34;deleted&#34;:false,&#34;required_participants&#34;:[{&#34;name&#34;:&#34;mrshake &#34;,&#34;id&#34;:42902,&#34;user_id&#34;:0,&#34;email&#34;:&#34;[email protected]&#34;}],&#34;start_date&#34;:&#34;2014-11-07T11:49:37Z&#34;}]
</code></pre>

<p>当我与 Charles 核对请求时,它仍然是正确的。这是查尔斯的截图。
<img src="/image/k3XLQ.png" alt="enter image description here"/> </p>

<p>但在服务器上,访问方式为</p>

<pre><code>data=&#34;{\&#34;meetings\&#34;:[{\&#34;id\&#34;:91160,\&#34;total_topics\&#34;:0,\&#34;optional_participants\&#34;:[],\&#34;readonly_participants\&#34;:[],\&#34;finish_time\&#34;:\&#34;2014-11-07T13:47:17Z\&#34;,\&#34;location\&#34;:\&#34;\&#34;,\&#34;finish_date\&#34;:\&#34;2014-11-07T13:47:17Z\&#34;,\&#34;sample\&#34;:false,\&#34;start_time\&#34;:\&#34;2014-11-07T12:47:17Z\&#34;,\&#34;title\&#34;:\&#34;his 8898997998897897\&#34;,\&#34;updated_at\&#34;:\&#34;2014-11-07T12:47:27Z\&#34;,\&#34;tags\&#34;:[],\&#34;created_at\&#34;:\&#34;2014-11-07T12:47:17Z\&#34;,\&#34;lock\&#34;:false,\&#34;organizer\&#34;:{\&#34;name\&#34;:\&#34;Rehmat Qadir\&#34;,\&#34;id\&#34;:12891,\&#34;user_id\&#34;:0,\&#34;email\&#34;:\&#34;[email protected]\&#34;},\&#34;meeting_topics\&#34;:[],\&#34;deleted\&#34;:false,\&#34;required_participants\&#34;:[{\&#34;name\&#34;:\&#34;Rehmat Qadir\&#34;,\&#34;id\&#34;:12891,\&#34;user_id\&#34;:0,\&#34;email\&#34;:\&#34;[email protected]\&#34;}],\&#34;start_date\&#34;:\&#34;2014-11-07T12:47:17Z\&#34;}]}&#34;
</code></pre>

<p>反斜杠被添加到任何地方。服务器端无法将 <code>data</code> 的值视为 JSON 对象,无法解析数据。
知道实际问题是什么吗?任何帮助表示赞赏。</p>

<p>这就是我创建 <code>jsonString</code></p> 的方式

<pre><code>+ (NSString *)jsonFromArray:(NSArray *)meetings {
    NSArray *requestDescriptors = [ requestDescriptors];
    RKRequestDescriptor *requestDescriptorObject = ;

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

    if (meetings.count &gt; 0) {
      for (DBMeetings *meeting in meetings) {
            NSDictionary *parametersForObject = ;
            NSDictionary *meetingDictionary = ;
            ;
      }
    }

    ;
    NSData *jsonData = ;

    NSString *jsonString;
    if(jsonData) {jsonString = [ initWithData:jsonData encoding:NSUTF8StringEncoding];}
    return jsonString;
}
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>您创建的 JSON 字符串看起来不错,您发送它的方式和 Charles 日志也是如此。因此,这很可能是一个完全基于服务器的问题,涉及文本处理和内容转义。这通常应该是透明的,因此您需要展示它是如何处理的。更糟糕的是,您可以处理字符串并替换所有转义字符。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - RestKit 使用字典参数发布 multipartFormRequest 在数据中添加反斜杠,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/26806296/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/26806296/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - RestKit 使用字典参数发布 multipartFormRequest 在数据中添加反斜杠