is it will be a good practice to call API with json object as input parameter rather than NSDictionary.
以json对象作为输入参数而不是NSDictionary来调用API是一个好习惯。
Usually we used to send NSDictionary as input parameters. Is there any issue or performance improvement in mobile device or in server side
通常我们用来发送NSDictionary作为输入参数。移动设备或服务器端是否存在任何问题或性能改进
1 个解决方案
#1
2
Yes, it is a fine practice to take your Objective-C NSDictionary
, use NSJSONSerialization
to convert that to JSON (or use a library like AFNetworking that can do that for you) and send the JSON in the network request you send when calling your web service. You would generally not send the NSDictionary
itself (e.g. a plist or keyed archiver). JSON is the lingua franca of web services. (XML is another very common format, though JSON is easier on the iOS side, IMHO.)
是的,采用Objective-C NSDictionary,使用NSJSONSerialization将其转换为JSON(或使用可以为您执行此操作的AFNetworking等库)并在调用Web时发送的网络请求中发送JSON,这是一个很好的做法。服务。您通常不会发送NSDictionary本身(例如plist或keyed archiver)。 JSON是Web服务的通用语言。 (XML是另一种非常常见的格式,虽然JSON在iOS方面更容易,恕我直言。)
If you're building the network request yourself (e.g. building a NSMutableURLRequest
to be sent using NSURLSession
), remember to:
如果您自己构建网络请求(例如,构建使用NSURLSession发送的NSMutableURLRequest),请记住:
- set
HTTPMethod
to@"POST"
; - 将HTTPMethod设置为@“POST”;
- set the
HTTPBody
to be the JSON; - 将HTTPBody设置为JSON;
- set the
Content-Type
HTTP header toapplication/json
; and - 将Content-Type HTTP标头设置为application / json;和
- set the
Accept
HHTP header to indicate what format you're expecting the response to be (also likelyapplication/json
). - 设置Accept HHTP标头以指示您期望响应的格式(也可能是application / json)。
#1
2
Yes, it is a fine practice to take your Objective-C NSDictionary
, use NSJSONSerialization
to convert that to JSON (or use a library like AFNetworking that can do that for you) and send the JSON in the network request you send when calling your web service. You would generally not send the NSDictionary
itself (e.g. a plist or keyed archiver). JSON is the lingua franca of web services. (XML is another very common format, though JSON is easier on the iOS side, IMHO.)
是的,采用Objective-C NSDictionary,使用NSJSONSerialization将其转换为JSON(或使用可以为您执行此操作的AFNetworking等库)并在调用Web时发送的网络请求中发送JSON,这是一个很好的做法。服务。您通常不会发送NSDictionary本身(例如plist或keyed archiver)。 JSON是Web服务的通用语言。 (XML是另一种非常常见的格式,虽然JSON在iOS方面更容易,恕我直言。)
If you're building the network request yourself (e.g. building a NSMutableURLRequest
to be sent using NSURLSession
), remember to:
如果您自己构建网络请求(例如,构建使用NSURLSession发送的NSMutableURLRequest),请记住:
- set
HTTPMethod
to@"POST"
; - 将HTTPMethod设置为@“POST”;
- set the
HTTPBody
to be the JSON; - 将HTTPBody设置为JSON;
- set the
Content-Type
HTTP header toapplication/json
; and - 将Content-Type HTTP标头设置为application / json;和
- set the
Accept
HHTP header to indicate what format you're expecting the response to be (also likelyapplication/json
). - 设置Accept HHTP标头以指示您期望响应的格式(也可能是application / json)。