简体   繁体   English

AFNetworking-带有JSON参数的POST方法:var = {JSON}

[英]AFNetworking - POST Method with JSON parameter: var = {JSON}

I'm using a webservice that accepts POST method like this 我正在使用接受这样的POST方法的Web服务

KEY d ; 密钥d; VALUE {JSON REQUEST} VALUE {JSON请求}

for example (login): 例如(登录):

d={ "action": [ "login" ], "request_data": { "login": { "user": "user", "password": "password" } } } d = {“ action”:[“ login”],“ request_data”:{“ login”:{“ user”:“ user”,“ password”:“ password”}}}

If I use AFHTTPClient the server doesn't find any "d" parameter. 如果我使用AFHTTPClient,则服务器找不到任何“ d”参数。 I've tried to create dictionaries with all parameters, serialize via NSJSONSERIALIZE and then wrap everything in a NSMutabliDictionary *parmas with key-value @"d", params to pass to [httpClient postPath[....] ] but with no success. 我尝试创建具有所有参数的字典,通过NSJSONSERIALIZE进行序列化,然后将所有内容包装在NSMutabliDictionary * parmas中,键值为@“ d”,参数传递给[httpClient postPath [....]],但没有成功。 How can I do? 我能怎么做?

NSString *jsonString  = @"YourStringFromServer";
NSDictionary *temDic = [jsonString JSONValue]; // convert string Value to JSON and add it to Dictionary.
NSlog(@"%@",temDic);// set specific key for specific value.

It's looking for a JSON string. 它正在寻找JSON字符串。 I'm having a bit of trouble understanding the last couple of sentences in your question (you're not describing the steps very clearly) but basically: 我在理解问题中的最后几句话时遇到了一些麻烦(您没有很清楚地描述步骤),但是基本上是:

//Create your params
NSDictionary *parametersDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                                                    yourData, @"action",
                                                                    someMoreData, @"request_data", nil];

NSString *parametersJSONString = [parametersDictionary JSONString];

NSDictionary *parametersToSend = [NSDictionary dictionaryWithObject:parametersJSONString forKey:@"d"];

And you should be good to go. 而且您应该很好。 Note that JSONString requires JSONKit. 请注意, JSONString需要JSONKit。 Also, debug/log parametersToSend before sending to make sure you've got the correct data and that the problem doesn't lie elsewhere. 另外,在发送之前调试/记录parametersToSend以确保您具有正确的数据并且问题不在其他地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM