NSArray 返回字符串而不是字典 [英] NSArray Returning String Instead of Dictionary
问题描述
我已经设置了日志并检查了代码,并且 NSArray 作为字符串而不是字典返回.问题是我不确定如何将这个特定的字符串转换成字典,而且我的时间很紧.
代码如下:
NSArray* placeJson = [NSJSONSerialization JSONObjectWithData:jsonResponse options:kNilOptions error:nil];for (NSDictionary *placeJsons in placeJson) {NSLog(@"%@",NSStringFromClass([placeJsons class]));page = placeJsons[@"page"];NSLog(@"%d",page);如果(页面> 0){NSDictionary* pJson = placeJsons[@"data"];for (NSDictionary *dict in pJson) {
NSlog 的日志和结果错误:
2013-09-12 19:50:55.117 GetDeal[1584:c07] __NSCFString2013-09-12 19:50:55.118 GetDeal[1584:c07] -[__NSCFString objectForKeyedSubscript:]:无法识别的选择器发送到实例 0xac5f8902013-09-12 19:50:55.119 GetDeal[1584:c07] *** 由于未捕获的异常NSInvalidArgumentException"而终止应用程序,原因:-[__NSCFString objectForKeyedSubscript:]:无法识别的选择器发送到实例 0xac5f890"
此代码也可能有用.
NSLog(@"AES:%@",aesResponse);NSData* jsonResponse = [aesResponse dataUsingEncoding:NSUTF8StringEncoding];NSArray* placeJson = [NSJSONSerialization JSONObjectWithData:jsonResponse options:kNilOptions error:nil];
对不起.JSON 数据:
http://mobapps.getdeal.me/getcoupons.php?category=art
可能的超级解决方案:
NSArray* placeJson = [NSJSONSerialization JSONObjectWithData:jsonResponse options:kNilOptions error:nil];NSDictionary *placeJsons = [NSJSONSerialization JSONObjectWithData:[placeJson dataUsingEncoding:NSUTF8StringEncoding]
错误信息.
NSArray"没有可见的@interface 声明了选择器dataUsingEncoding:"
非常抱歉……但大约 30 分钟后,我的生活将发生巨大变化.
回答马丁:
NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:NO];NSString *postLength = [NSString stringWithFormat:@"%d", [帖子长度]];**NSURL *url = [NSURL URLWithString:placeLink];**NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];[请求 setHTTPMethod:@"POST"];
配置文件
#define placeLink @"http://mobapps.getdeal.me/getcoupons3.php"
我会尝试一下.正如我在评论中所说,您的提供商发送嵌套 JSON":
NSURL *url = [NSURL URLWithString:@"http://mobapps.getdeal.me/getcoupons.php?category=art"];NSData *jsonResponse = [NSData dataWithContentsOfURL:url];NSLog(@"%@", [[NSString alloc] initWithData:jsonResponse encoding:NSUTF8StringEncoding]);
输出:
<前>"{\"data\":[[{\"PID\":\"90205\",\"PName\":null, ... ,\"page\":\"1\"}"这是再次编码为 JSON 字符串的 JSON 数据.第一个任务是删除外层 JSON 编码:
NSError *error;NSString *outerJson = [NSJSONSerializationJSONObjectWithData:jsonResponse选项:NSJSONReadingAllowFragments 错误:&error];NSLog(@"%@",outerJson);
输出:
<前>{"data":[[{"PID":"90205","PName":null, ... ,"page":"1"}现在我们有了正确的 JSON 数据,可以反序列化:
NSDictionary *innerJson = [NSJSONSerializationJSONObjectWithData:[outerJson dataUsingEncoding:NSUTF8StringEncoding]选项:0 错误:&error];NSLog(@"%@",innerJson);
输出:
<前>{数据 = (({PAddr = "在收尾部分选择艺术品可节省 20-85%.只需点击链接,然后点击页面底部的收尾绘画\".无需代码,价格如标记.";PCity = "";...率 = 20;骚 = 20;};});页 = 1;}I've setup logs and checked the code and NSArray is being returned as a string instead of a dictionary. The problem is that I'm not sure how I can convert this particular string into a dictionary and I'm running on a tight timeframe.
Here's the code:
NSArray* placeJson = [NSJSONSerialization JSONObjectWithData:jsonResponse options:kNilOptions error:nil];
for (NSDictionary *placeJsons in placeJson) {
NSLog(@"%@",NSStringFromClass([placeJsons class]));
page = placeJsons[@"page"];
NSLog(@"%d",page);
if (page > 0) {
NSDictionary* pJson = placeJsons[@"data"];
for (NSDictionary *dict in pJson) {
Error in Logs and results of NSlog:
2013-09-12 19:50:55.117 GetDeal[1584:c07] __NSCFString
2013-09-12 19:50:55.118 GetDeal[1584:c07] -[__NSCFString objectForKeyedSubscript:]: unrecognized selector sent to instance 0xac5f890
2013-09-12 19:50:55.119 GetDeal[1584:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString objectForKeyedSubscript:]: unrecognized selector sent to instance 0xac5f890'
This code may be useful as well.
NSLog(@"AES:%@",aesResponse);
NSData* jsonResponse = [aesResponse dataUsingEncoding:NSUTF8StringEncoding];
NSArray* placeJson = [NSJSONSerialization JSONObjectWithData:jsonResponse options:kNilOptions error:nil];
Sorry. The JSON data:
http://mobapps.getdeal.me/getcoupons.php?category=art
Possible supe solution:
NSArray* placeJson = [NSJSONSerialization JSONObjectWithData:jsonResponse options:kNilOptions error:nil];
NSDictionary *placeJsons = [NSJSONSerialization JSONObjectWithData:[placeJson dataUsingEncoding:NSUTF8StringEncoding]
Error Message.
No visible @interface for 'NSArray' declares the selector 'dataUsingEncoding:'
Definitely sorry... but in about 30 minutes my life is going to change dramatically.
Answer to Martin:
NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:NO];
NSString *postLength = [NSString stringWithFormat:@"%d", [post length]];
**NSURL *url = [NSURL URLWithString:placeLink];**
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
[request setHTTPMethod:@"POST"];
Config.h
#define placeLink @"http://mobapps.getdeal.me/getcoupons3.php"
I will give it a try. As I said in a comment, your provider sends "nested JSON":
NSURL *url = [NSURL URLWithString:@"http://mobapps.getdeal.me/getcoupons.php?category=art"];
NSData *jsonResponse = [NSData dataWithContentsOfURL:url];
NSLog(@"%@", [[NSString alloc] initWithData:jsonResponse encoding:NSUTF8StringEncoding]);
Output:
"{\"data\":[[{\"PID\":\"90205\",\"PName\":null, ... ,\"page\":\"1\"}"
This is JSON data once more encoded as a JSON string. The first task is to remove the outer JSON encoding:
NSError *error;
NSString *outerJson = [NSJSONSerialization
JSONObjectWithData:jsonResponse
options:NSJSONReadingAllowFragments error:&error];
NSLog(@"%@", outerJson);
Output:
{"data":[[{"PID":"90205","PName":null, ... ,"page":"1"}
So now we have proper JSON data, which can be de-serialized:
NSDictionary *innerJson = [NSJSONSerialization
JSONObjectWithData:[outerJson dataUsingEncoding:NSUTF8StringEncoding]
options:0 error:&error];
NSLog(@"%@", innerJson);
Output:
{ data = ( ( { PAddr = "Save 20-85% on select artwork in the closeout section. Just click the link, then click \"paintings on closeout\" toward the bottom of the page. No code necessary, prices are as marked."; PCity = ""; ... rate = 20; sao = 20; }; } ); page = 1; }
这篇关于NSArray 返回字符串而不是字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!