- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
嗨,我在使用 NSData
将 NSDictionary
转换为 NSJSONSerialization
时遇到问题?
我使用我的代码获取数据,但无法将其转换为 json。
这是我的代码...
ViewController.h 文件
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<NSURLConnectionDelegate>
{
NSString *massage;
NSURL *url;
NSMutableURLRequest *request;
NSURLConnection *connection;
NSMutableData *httpbody;
NSMutableData *webData;
NSData *responceData;
NSDictionary *responceJson;
}
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSString *mobile = @"123456789";
NSString *password = @"Welcome123";
NSString *deviceId = @"123sdfg15641ert321ret";
url = [NSURL URLWithString:@"http://203.109.87.34:8585/d-cab/web/app_dev.php/api/v1"];
massage = [NSString stringWithFormat:@"{\"action\":\"login\",\"data\":{\"contact\":\"%@\",\"password\":\"%@\",\"deviceId\":\"%@\"}}",mobile,password,deviceId];
//NSLog(@"Body = %@", massage);
httpbody = [ NSMutableData dataWithBytes: [ massage UTF8String ] length: [ massage length ] ];
request = [NSMutableURLRequest requestWithURL: url];
[request setHTTPMethod: @"POST"];
[request setHTTPBody: httpbody];
[request setTimeoutInterval:10.0];
connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
//NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];
//[connection start];
if (connection) {
webData = [[NSMutableData alloc]init];
}
}
#pragma mark NSURLConnection Delegate Methods
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[webData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[webData appendData:data];
//NSLog(@"WebData = %@",webData);
//NSLog(@"=================================");
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSError* error = nil;
responceJson = [NSJSONSerialization JSONObjectWithData:webData options:kNilOptions error:&error];
NSLog(@"ResponceJSON = %@",responceJson);
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
// Show error message
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Error In connection" message:@"We are facing some problem in connection. Please Check your Internet Connection." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[alert show];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
最佳答案
嘿,感谢所有关注我问题的人......但我在我的问题中发现了问题。
我正在尝试将参数发送为字符串格式而不是字典格式,并且未指定要请求的 header 。这是我更新的代码...
NSDictionary *paramData = [[NSDictionary alloc]initWithObjectsAndKeys:mobile,@"contact",password,@"password",deviceId,@"deviceId", nil];
//NSLog(@"paramData = %@",paramData);
jsonParam = [[NSDictionary alloc]initWithObjectsAndKeys:@"login",@"action",paramData,@"data", nil];
//NSLog(@"jsonParam = %@",jsonParam);
httpbody = [NSJSONSerialization dataWithJSONObject:jsonParam options:0 error:nil];
request = [NSMutableURLRequest requestWithURL: url];
request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10.0];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%d", [httpbody length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody: httpbody];
connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (connection) {
webData = [[NSMutableData alloc]init];
}
关于ios - 如何使用 NSJSONSerialization 将 NSData 转换为 NSDictionary?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33388425/
我正在使用NSJSONSerialization将json字符串转换为NSDictionaray JSON字符串是 {"bid":88.667,"ask":88.704} 在NSJSONSeriali
我正在使用 iOS 5 新功能来解析 JSON,但我不知道为什么我没有得到任何键值对。 “aStr”(数据的字符串表示形式)将正确的 JSON 放在输出窗口上,但我在“dicData”中什么也没有得到
我正在尝试将 JSON 文件解析为 NSArray,对于正数来说一切都很好。然而,该 JSON 文件中的所有负整数都会生成高数字,例如“[11] __NSCFNumber * (long)720575
我有一个NSJSONSerialization JSONObjectWithData,我在其中传递了NSData对象。结果,我得到一个NSString而不是NSDictionary。我的完整行是: N
在以下用于解析JSON的代码中,当对象碰巧不是NSArray或NSDictionary时,将使用NSJSONReadingAllowFragments。 下面的代码可以正常工作并输出32。但是如果将3
好的,我正在使用以下代码获取 JSON 文件 - (void)dataRequest { NSURL *dataURL = [NSURL URLWithString:@"link to JSO
我有一些看起来像这样的代码: NSDictionary* json = [NSJSONSerialization JSONObjectWithData:data
我在尝试从 URL 解析 JSON 时遇到错误。 代码如下: override func viewDidLoad() { super.viewDidLoad()
我在 AppDelegate.m 中有这段代码: NSError *error; NSString *filePath = [[NSBundle mainBundle] pathFor
这个问题在这里已经有了答案: NSJSONSerialization and Emoji (2 个答案) 关闭 6 年前。
我正在使用 JSON 来填充带有标记的 mapView。 在方法 -(void)connectionDidFinishLoading:(NSURLConnection *)connection 编译器
我在使用 NSJSONSerialization 创建 NSString 的 NSDictionary 表示(JSON 字符串)时遇到了一些问题。我以前使用过 JSONKit,但由于它在 iOS9 中
请原谅我可能犯的错误,我第一次问这个问题。 阅读本网站上的几个主题后,like this one首先我会尝试使用 describe 方法,但它仍然不起作用@all :-( 我的 .json 文件看起来
我的 iPad 应用程序需要解析一个 10 MB 的 JSON 文件并将其内容保存在 Core Data 中。 考虑到大文件大小和内存使用,您会推荐使用 NSJSONSerialization 还是您
我有一个看起来像这样的字符串:(它派生自一个格式略有错误的解码数据对象,我通过控制台修复并验证了这个输出) {"home":{"breakfast":{"protein":"55","carbs":"
我已经在几个项目中使用过它... [NSJSONSerialization dataWithJSONObject:someObject options:0 error:nil] 但我不知道如何指定无选
我有一本字典,当我登录时它会显示... { Date = "2013-04-30 17:17:18 +0000"; Description = Kb; EventID = "9
在我的 iOS 应用程序中,我在后台线程中做一些工作(使用 performSelectorInBackground)。在此线程中,我使用 NSJSONSerialization 及其类方法来解析 JS
我正在使用 NSJSONSerializer 来解析一些 json 格式的数据。数据包含一些“ü”和“ö”等。我发现它确实返回 nil,如果没有事先使用 NSASCIIStringEncoding 将
我在使用 NSJSONSerialization 从 Met Office Datapoint API 读取 JSON 时遇到问题。 出现以下错误 Error Domain=NSCocoaErrorD
我是一名优秀的程序员,十分优秀!