AFNetworking POST请求中的JSON数据混乱 [英] JSON data messed up in AFNetworking POST request

查看:174
本文介绍了AFNetworking POST请求中的JSON数据混乱的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过AFNetworking发送一个关于Objective-C的请求.当我NSLog参数时,这是我要发送的对象:

I am sending a request with AFNetworking for Objective-C. When I NSLog the parameters this is the object I am sending:

games = (
        {
        id = 50;
        p = 8;
        ts = 0;
        tt = ();
        tw = 0;
        ys = 35150;
        yt = {
            156424496 = "37.416669";
            156609008 = "56.661210";
            ....
            252846816 = "7.075133";
            252856944 = "61.329850";
        };
        yw = 0;
    }, ...

这是服务器收到的信息.

This is what the server receives.

games = (
        {id = 50;},
        {p = 8;},
        {ts = 0;},
        {tw = 0;},
        {ys = 35150;},
        {
            yt = {156424496 = "37.416669";};
        },
        {
            yt = {156609008 = "56.661210";};
        },
        ...
        {
            yt = {252846816 = "7.075133";};
        },
        {
            yt = {252856944 = "61.329850";};
        },
        {yw = 0;},
...

就好像它要占用我对象的每个属性并用它创建一个新对象一样.更糟糕的是,它要获取数组中的多个对象,并将所有对象的所有属性放到数组的同一深度上,然后将它们变成单独的对象.

It is as if it is taking each property of my object and creating a new object with it. The worse part is that it's taking the multiple objects that are in the array and putting all properties of all objects and turning them into separate object on the same depth of the array.

这是我用来发送此代码的代码:

Here is the code I am using to send this off:

NSArray *games = [ResourceManager getAllGames];
NSMutableArray *gamesArray = [[NSMutableArray alloc] initWithCapacity:[games count]];
for(Game *g in games)
{
    [gamesArray addObject:[g toDictionary]];
}
User *user = [ResourceManager getUser];
NSDictionary *params = [[NSDictionary alloc] initWithObjectsAndKeys:gamesArray, @"games", user.id, @"user_id", nil];
NSLog(@"PARAMS: %@", params); <- this is the first block of code above
[self postPath:API_SYNC_GAMES_URL parameters:params success:^(AFHTTPRequestOperation *operation, id JSON)
{
}

我一直无法弄清楚为什么会发生这种情况,我全都没有猜测.如果有人可以向我指出正确的方向,将不胜感激.

I have not been able to figure out why this would be happening, and I am all out of guesses. If someone could point me in the right direction it would be very appreciated.

更新

如果我发布的是single object而不是对象数组,则它将成功到达服务器.

If I post a single object rather than an array of the objects it arrives at the server successfully.

推荐答案

我能够通过使用NSDictionary而不是数组来解决此问题.我拥有的每个对象都有一个唯一的键,因此我将其用于NSDictionary,如下所示:

I was able to solve this issue by using an NSDictionary instead of an array. Each object I have has a unique key so I used that key for the NSDictionary like so:

NSMutableDictionary *gamesArray = [[NSMutableDictionary alloc] 
                                          initWithCapacity:[games count]];
for(Game *g in games)
{
    [gamesArray setObject:[g toDictionary] 
                   forKey:[NSString stringWithFormat:@"%@", g.id]];
}

这似乎已经解决了问题.

That seems to have solved the issue.

这篇关于AFNetworking POST请求中的JSON数据混乱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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