JSON解析与iOS 6和Xcode的4.5.2
我在Objective-C和和建设网页的标志特定网站新。当用户提供信息下面的JSON从服务器返回回:JSON解析与iOS 6和Xcode的4.5.2
{
"object":{},
"resultCode":0,
"resultMessage":"You successfully signed in "
}
而这正是我试图做基础的教程,我发现:
-(IBAction)login{
// Download JSON
NSData *data = [NSData dataWithContentsOfURL: [NSURL URLWithString:[NSString stringWithFormat:@"https://www.example.com/login?username=%@&password=%@", nationalID.text,passowrd.text]]];
NSError *error;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSArray *response = [json objectForKey:@"resultMessage"];
NSLog(@"response %@",response);
}
我要退出“ resultMessage”但不知何故,它抛出一个异常:
2012-12-13 06:46:50.607 myProject[7178:11303] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'data parameter is nil'
*** First throw call stack:
(0x1c8d012 0x10cae7e 0x1c8cdeb 0xbff817 0x2f1a 0x10de705 0x15920 0x158b8 0xd6671 0xd6bcf 0xd5d38 0x4533f 0x45552 0x233aa 0x14cf8 0x1be8df9 0x1be8ad0 0x1c02bf5 0x1c02962 0x1c33bb6 0x1c32f44 0x1c32e1b 0x1be77e3 0x1be7668 0x1265c 0x279d 0x26c5)
libc++abi.dylib: terminate called throwing an exception
,如果你能看到问题描述:
“NSInvalidArgumentException”的,理由是:“数据参数是零”
指从网站的网址您回应的回应是零或NULL
这是不相关的JSON解析器问题。
感谢您的尝试 – Timmy
看起来像-dataWithContentsOfURL:消息失败并返回nil,这正是错误消息所说的内容。您需要确认您的-stringWithFormat:消息正在为您提供正确的URL格式,并检查服务器是否正在返回您所期望的。
谢谢,这解决了我的问题。 – Timmy
看起来你有一个简单的拼写错误,您正在使用:
nationalID.text,passowrd.text
你大概意思不是拼错的“密码”。
一次尝试静态URL,如:
如果你返回正确的响应,然后尝试属性变量,如:
0你已经https://www.example.com/login?username=self.username.text&password=self.password.text
而且一个错误是,resultMessage是不是字符串数组。
希望这可以帮到你
如果你确实找到了解决方案,请发布它。谢谢。 – androniennn