简体   繁体   English

获取NSData中的json

[英]Get the json which is in NSData

I'm new in Objective-c. 我是Objective-c的新手。 I want to make an HTTPRequest to an API. 我想对API发出HTTPRequest。 This works, I get my response in an NSData object. 这有效,我在NSData对象中得到响应。 This response is a json response. 此响应是json响应。 How can I parse my response to get a dictionary with key: value format? 如何解析我的响应以获取具有key:value格式的字典?

I used this code to get the data: 我使用以下代码获取数据:

  data = [NSURLConnection sendSynchronousRequest: req
                             returningResponse: nil
                                         error: nil];

Thanks 谢谢

Use NSJSONSerialization (iOS5+) to convert JSON strings to Cocoa objects ( NSDictionary in your case) and vice-versa. 使用NSJSONSerialization (iOS5 +)将JSON字符串转换为Cocoa对象(在您的情况下为NSDictionary ),反之亦然。

PS : If you need to also support iOS versions before 5.0, namely where the NSJSONSerialization is not available, there are third-party libraries out there, like JSONKit for example. PS:如果您还需要支持5.0之前的iOS版本,即NSJSONSerialization不可用,则那里有第三方库,例如JSONKit You may then either always use JSONKit both on iOS4 and iOS5, or your may test at runtime for NSJSONSerialization availability and use it if available (but fallback to JSONKit if not). 然后,您可以始终在iOS4和iOS5上同时使用JSONKit ,或者可以在运行时测试NSJSONSerialization可用性,并在可用时使用它(如果不可用,则回JSONKit )。

(For more details on cross-SDK development, like making and app compatible with iOS4 but being able to use iOS5 classes when available, see the SDK Compatibility Guide ) (有关跨SDK开发的更多详细信息,例如与iOS4兼容并与之兼容的应用程序,但能够在可用时使用iOS5类,请参阅SDK兼容性指南


By the way, you should (must) avoid using synchronous URL requests. 顺便说一句,您应该(必须)避免使用同步URL请求。 This will block your thread until it receives the response from the network. 这将阻塞您的线程,直到它接收到来自网络的响应。 If you execute it on the main thread, your whole UI will be frozen until you receive the response, which can take several seconds in a mobile environment (where you don't always have the best network coverage). 如果您在主线程上执行它,则整个UI将被冻结,直到您收到响应为止,这在移动环境中(可能并不总是具有最佳的网络覆盖范围)可能要花费几秒钟。

Prefer: 身高:

  • using the API of NSURLConnection that uses a delegate (See the URL Loading System Programming Guide for details), 使用使用委托的NSURLConnection API(有关详细信息,请参见《 URL加载系统编程指南 》),
  • or using the newly introduced sendAsynchronousRequest:queue:completionHandler: method (iOS5+) that will execute the request in a background NSOperation, 或使用新引入的sendAsynchronousRequest:queue:completionHandler:方法(iOS5 +)将在后台NSOperation中执行请求,
  • or use some third-party framework that will make your work with requests easier (I recommend AFNetworking for that, which is also great for communicating with WebServices and automatically convert the response to a JSON object, uses blocks to handle the response asynchronously and make your code easier to write and understand, and much more) 或使用一些第三方框架来简化您的请求处理(我建议为此使用AFNetworking ,它也非常适合与WebServices通信并自动将响应转换为JSON对象,使用块异步处理响应并使您代码更容易编写和理解,以及更多)

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

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