将分层 NSDictionary 作为 POST 请求发送
我有一个方法可以将 NSDictionary 转换为 POST 参数字符串,以便发送到我们正在开发的应用程序中使用的 API。它可以正确转换 NSString
、NSDate
和 NSArray
。效果很好。
现在我遇到了一种情况,我的字典中的一个对象实际上可能是另一个字典,我想知道如何根据 POST 请求来处理它。你们如何处理分层数据结构?我想过为每个字典发送多个请求,但我不确定是否没有更好的方法。
谢谢
-f
I've got method that converts NSDictionary
s to POST argument strings for sending to an API we use in an app we're working on. It properly converts NSString
s, NSDate
s and NSArray
s. That works just fine.
Now I've got a situation where one of the objects in my dictionary might actually be another dictionary and I was wondering how to handle that in terms of the POST request. How do you guys handle hierarchical data structures? I thought of sending multiple requests for each dictionary but I'm not sure if there's not a better way.
Thanks
-f
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
就我个人而言,我只是将整个 NSDictionary “JSONify”为字符串,并将其作为带有 Content-Type: application/json 的 HTTP 请求正文发送,而无需进一步对其进行编码。但是,正如您所建议的,您可以将该值转换为 JSON 结构以作为参数发送;一个潜在的缺点是,它可能涉及大量编码/解码,具体取决于您发送的数据类型。
Personally, I would just "JSONify" the entire NSDictionary as a string and send that as HTTP request body with a Content-Type: application/json without further encoding it. However, as you suggested, you could convert the value as a JSON structure to send as an argument; one potential drawback is that it could involve a lot of encoding/decoding depending on what type of data you send.