AFNetworking将JSON数组发布为多个单项词典 [英] AFNetworking posts JSON arrays as multiple single-entry dictionaries

查看:130
本文介绍了AFNetworking将JSON数组发布为多个单项词典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到的问题与上,Mattt说:您所描述的结构无法确定用查询字符串编码表示。我正在使用POST,因此不涉及查询字符串。但也许POST数据存在限制?


  • 我也很好奇为什么包含 constructBodyWithBlock的AFNetworking调用时间越长成功,而较短的一个失败。但是,这个答案对我来说并不那么重要。更长的方法几乎可以工作,如果它返回我发送的相同JSON,我会很乐意使用它。


  • 全部谢谢!

    解决方案


    我正在使用POST,因此不涉及查询字符串。但也许POST数据存在限制?


    限制不在POST上,它是URL表单编码。我继续说你应该编码为JSON,你可以对你的经理进行以下配置更改:



    manager.requestSerializer = [AFJSONRequestSerializer序列化程序];



    此外,除非您实际构建多部分请求,否则请勿使用 constructBodyWithBlock 该方法的版本。


    I'm having a similar issue to the one discussed here.

    I'm attempting to post JSON to a server.

    Here's the Objective-C code that should work, but doesn't. I get an empty array in the response object, not sure why:

        AFHTTPRequestOperation * operation = [manager POST:uploadScriptUrl
          parameters:mutableJSON
          success:^(AFHTTPRequestOperation * operation, id responseObject) {
              successBlock(operation, responseObject);
          }
          failure:^(AFHTTPRequestOperation * operation, NSError * error) {
              failureBlock(operation, error);
          }];
    

    This code (which is adapted from code I use to upload images) KIND OF works. I know it's definitely not the approved way to do it:

        AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
        AFJSONRequestSerializer * requestSerializer = [AFJSONRequestSerializer serializer];
        [requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
        [requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
        manager.requestSerializer = requestSerializer;
    
        AFHTTPRequestOperation * operation = [manager POST:uploadScriptUrl
        parameters:mutableJSON //passed in
        constructingBodyWithBlock:^(id <AFMultipartFormData> formData) {
            //Do nothing
       }
        success:^(AFHTTPRequestOperation * operation, id responseObject) {
            successBlock(operation, responseObject);
        }
        failure:^(AFHTTPRequestOperation * operation, NSError * error) {
            failureBlock(operation, error);
        }];
    

    It posts my JSON to the server, but in the process of formulating the request, the JSON is mangled.

    Here's how it starts out:

    {
    "key1": [
        {
            "dictionary1": {
                "param1": "value1",
                "param2": "value2",
                "array1A": [
                    "value1",
                    "value2"
                ],
                "array1B": [
                    "value1",
                    "value2"
                ]
            }
        }
    ]
    

    }

    and here's what AFNetworking sends to the server:

    {
    "key1": [
        {
            "dictionary1": {
                "array1A": [
                    "value1"
                ]
            }
        },
        {
            "dictionary1": {
                "array1A": [
                    "value2"
                ]
            }
        },
        {
            "dictionary1": {
                "array1B": [
                    "value1"
                ]
            }
        },
        {
            "dictionary1": {
                "array1B": [
                    "value2"
                ]
            }
        },
        {
            "dictionary1": {
                "param1": "value1"
            }
        },
        {
            "dictionary1": {
                "param2": "value2"
            }
        }
    ]
    

    }

    Here's what Charles shows for the request. You can see how the JSON structure has already been altered in the request, before the server has touched the data.

    Here's the PHP I'm using on the server. Dead simple for now:

    <?php
    
    header('Content-type: application/json'); //Not sure if this is needed.
    
    $json_string = json_encode($_POST);
    
    header("HTTP/1.0 200 OK");
    echo $json_string;
    
    ?>
    

    So, all of that said, here are my questions:

    1. Does AFNetworking handle nested JSON arrays? On this page Mattt says: "The structure you're describing can't deterministically be represented with query string encoding." I'm using POST, so query strings are not involved. But maybe the limitation exists with POST data as well?

    2. I'm also curious why the longer AFNetworking call that includes constructingBodyWithBlock succeeds while the shorter one fails. However, this answer is less important to me. The longer method very nearly works and I'd be happy to use it if it returned the same JSON that I send.

    Thanks all!

    解决方案

    I'm using POST, so query strings are not involved. But maybe the limitation exists with POST data as well?

    The limitation is not on POST, it's URL form encoding. I go on to say that you should encode as JSON, which you can do with the following configuration change to your manager:

    manager.requestSerializer = [AFJSONRequestSerializer serializer];.

    Also, unless you're actually constructing a multi-part request, don't use the constructingBodyWithBlock version of that method.

    这篇关于AFNetworking将JSON数组发布为多个单项词典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆