我已经设置了日志并检查了代码,并且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);
if (page > 0) {
NSDictionary* pJson = placeJsons[@"data"];
for (NSDictionary *dict in pJson) {
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'
此代码也可能有用。
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]
错误讯息。
No visible @interface for 'NSArray' declares the selector 'dataUsingEncoding:'
绝对抱歉......但在大约30分钟后,我的生活将发生巨大变化。
回答马丁:
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"
答案 0 :(得分:2)
我会试一试。正如我在评论中所说,您的提供商发送“嵌套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 = [NSJSONSerialization
JSONObjectWithData:jsonResponse
options:NSJSONReadingAllowFragments error:&error];
NSLog(@"%@", outerJson);
输出:
{"data":[[{"PID":"90205","PName":null, ... ,"page":"1"}
现在我们有了适当的JSON数据,可以反序列化:
NSDictionary *innerJson = [NSJSONSerialization
JSONObjectWithData:[outerJson dataUsingEncoding:NSUTF8StringEncoding]
options:0 error:&error];
NSLog(@"%@", innerJson);
输出:
{ 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; }
答案 1 :(得分:1)
NSString *response = @"{\"data\":[[{\"PID\":\"88232\",\"PName\":null,\"PAddr\":\"Browse the jewelry, beads, components and more in the Clearance section for savings up to 80% and free standard shipping on all orders.\",\"PDistrict\":\"Clearance - Save 10% - 80% on Select Items + Free Shipping\",\"PCity\":\"\",\"PImage\":\"https:\\\/\\\/s3-eu-west-1.amazonaws.com\\\/getdeal\\\/images\\\/logos\\\/artbeads.jpg\",\"PPhone\":\"artbeads.com\",\"PPrice\":\"\",\"PLat\":\"http:\\\/\\\/www.getdeal.com\\\/gutschein-frameset\\\/?id=88232\",\"PLong\":\"http:\\\/\\\/www.getdeal.com\\\/gutschein-frameset\\\/?id=88232\",\"TID\":\"Art\",\"UID\":\"discount\",\"countlike\":\"%\",\"distance\":\"80\",\"sao\":\"80\",\"rate\":\"80\",\"liked\":\"80\"}],{\"1\":{\"PID\":\"112773\",\"PName\":null,\"PAddr\":\"Click on this link to get 25-75% discount on all featured art supplies on sale. No code needed. Some restrictions may apply. Includes free shipping on orders $150 or more. See site for more details.\",\"PDistrict\":\"25-75% off Featured Art Supplies on Sale\",\"PCity\":\"\",\"PImage\":\"https:\\\/\\\/s3-eu-west-1.amazonaws.com\\\/getdeal\\\/images\\\/logos\\\/the-art-store.jpg\",\"PPhone\":\"shoptheartstore.com\",\"PPrice\":\"\",\"PLat\":\"http:\\\/\\\/www.getdeal.com\\\/gutschein-frameset\\\/?id=112773\",\"PLong\":\"http:\\\/\\\/www.getdeal.com\\\/gutschein-frameset\\\/?id=112773\",\"TID\":\"Art\",\"UID\":\"discount\",\"countlike\":\"%\",\"distance\":\"75\",\"sao\":\"75\",\"rate\":\"75\",\"liked\":\"75\"}},{\"2\":{\"PID\":\"113774\",\"PName\":null,\"PAddr\":\"Get discounts of up to 75% on sale artworks. Simply go through this link, click the \\\"up to 75% off\\\" rotating banner to view the sale items. Restrictions apply.\",\"PDistrict\":\"15-75% off Sale Artworks\",\"PCity\":\"\",\"PImage\":\"https:\\\/\\\/s3-eu-west-1.amazonaws.com\\\/getdeal\\\/images\\\/logos\\\/art-com.jpg\",\"PPhone\":\"eu.art.com\",\"PPrice\":\"\",\"PLat\":\"http:\\\/\\\/www.getdeal.com\\\/gutschein-frameset\\\/?id=113774\",\"PLong\":\"http:\\\/\\\/www.getdeal.com\\\/gutschein-frameset\\\/?id=113774\",\"TID\":\"Art\",\"UID\":\"discount\",\"countlike\":\"%\",\"distance\":\"75\",\"sao\":\"75\",\"rate\":\"75\",\"liked\":\"75\"}},{\"3\":{\"PID\":\"109916\",\"PName\":null,\"PAddr\":\"Follow the link to get up to 72% savings on clearance items. Includes free shipping. Limited time offer.\",\"PDistrict\":\"17-72% off Clearance Items + Free Shipping\",\"PCity\":\"\",\"PImage\":\"https:\\\/\\\/s3-eu-west-1.amazonaws.com\\\/getdeal\\\/images\\\/logos\\\/overstock-art.jpg\",\"PPhone\":\"overstockart.com\",\"PPrice\":\"\",\"PLat\":\"http:\\\/\\\/www.getdeal.com\\\/gutschein-frameset\\\/?id=109916\",\"PLong\":\"http:\\\/\\\/www.getdeal.com\\\/gutschein-frameset\\\/?id=109916\",\"TID\":\"Art\",\"UID\":\"discount\",\"countlike\":\"%\",\"distance\":\"72\",\"sao\":\"72\",\"rate\":\"72\",\"liked\":\"72\"}},{\"4\":{\"PID\":\"90205\",\"PName\":null,\"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.\",\"PDistrict\":\"Save 20 - 85% in Closeout Section\",\"PCity\":\"\",\"PImage\":\"https:\\\/\\\/s3-eu-west-1.amazonaws.com\\\/getdeal\\\/images\\\/logos\\\/overstock-art.jpg\",\"PPhone\":\"overstockart.com\",\"PPrice\":\"\",\"PLat\":\"http:\\\/\\\/www.getdeal.com\\\/gutschein-frameset\\\/?id=90205\",\"PLong\":\"http:\\\/\\\/www.getdeal.com\\\/gutschein-frameset\\\/?id=90205\",\"TID\":\"Art\",\"UID\":\"discount\",\"countlike\":\"%\",\"distance\":\"85\",\"sao\":\"85\",\"rate\":\"85\",\"liked\":\"85\"}},{\"5\":{\"PID\":\"91321\",\"PName\":null,\"PAddr\":\"Get a 10-71% discount on the Deal of the Day. Just click the link, then click \\\"Art Deal of the Day\\\" toward the bottom right of the page.\",\"PDistrict\":\"Save 10 - 71% on Deal of the Day\",\"PCity\":\"\",\"PImage\":\"https:\\\/\\\/s3-eu-west-1.amazonaws.com\\\/getdeal\\\/images\\\/logos\\\/overstock-art.jpg\",\"PPhone\":\"overstockart.com\",\"PPrice\":\"\",\"PLat\":\"http:\\\/\\\/www.getdeal.com\\\/gutschein-frameset\\\/?id=91321\",\"PLong\":\"http:\\\/\\\/www.getdeal.com\\\/gutschein-frameset\\\/?id=91321\",\"TID\":\"Art\",\"UID\":\"discount\",\"countlike\":\"%\",\"distance\":\"71\",\"sao\":\"71\",\"rate\":\"71\",\"liked\":\"71\"}},{\"6\":{\"PID\":\"91872\",\"PName\":null,\"PAddr\":\"Click on the \\\"FINAL SALE\\\" tab on the upper right hand side of the page to get discounts of 30-50%. No code needed.\",\"PDistrict\":\"30-50% off in Final Sale Section\",\"PCity\":\"\",\"PImage\":\"https:\\\/\\\/s3-eu-west-1.amazonaws.com\\\/getdeal\\\/images\\\/logos\\\/tracy-porter.jpg\",\"PPhone\":\"blog.tracyporter.com\",\"PPrice\":\"\",\"PLat\":\"http:\\\/\\\/www.getdeal.com\\\/gutschein-frameset\\\/?id=91872\",\"PLong\":\"http:\\\/\\\/www.getdeal.com\\\/gutschein-frameset\\\/?id=91872\",\"TID\":\"Art\",\"UID\":\"discount\",\"countlike\":\"%\",\"distance\":\"50\",\"sao\":\"50\",\"rate\":\"50\",\"liked\":\"50\"}},{\"7\":{\"PID\":\"112155\",\"PName\":null,\"PAddr\":\"Click on this link to get a 15-34% discount on all clearance artwork. No code needed. Some restrictions may apply. See site for more details. Includes free shipping on orders $200 or more.\",\"PDistrict\":\"15-34% off Clearance Artworks\",\"PCity\":\"\",\"PImage\":\"https:\\\/\\\/s3-eu-west-1.amazonaws.com\\\/getdeal\\\/images\\\/logos\\\/art-addiction.jpg\",\"PPhone\":\"artaddictioninc.com\",\"PPrice\":\"\",\"PLat\":\"http:\\\/\\\/www.getdeal.com\\\/gutschein-frameset\\\/?id=112155\",\"PLong\":\"http:\\\/\\\/www.getdeal.com\\\/gutschein-frameset\\\/?id=112155\",\"TID\":\"Art\",\"UID\":\"discount\",\"countlike\":\"%\",\"distance\":\"34\",\"sao\":\"34\",\"rate\":\"34\",\"liked\":\"34\"}},{\"8\":{\"PID\":\"86847\",\"PName\":null,\"PAddr\":\"Enter this code at checkout to get a 40% discount on all orders with no minimum spend required. Free shipping included. Limited time offer.\",\"PDistrict\":\"40% off Sitewide + Free Shipping\",\"PCity\":\"TENTSALE40\",\"PImage\":\"https:\\\/\\\/s3-eu-west-1.amazonaws.com\\\/getdeal\\\/images\\\/logos\\\/overstock-art.jpg\",\"PPhone\":\"overstockart.com\",\"PPrice\":\"TENTSALE40\",\"PLat\":\"http:\\\/\\\/www.getdeal.com\\\/gutschein-frameset\\\/?id=86847\",\"PLong\":\"http:\\\/\\\/www.getdeal.com\\\/gutschein-frameset\\\/?id=86847\",\"TID\":\"Art\",\"UID\":\"discount\",\"countlike\":\"%\",\"distance\":\"40\",\"sao\":\"40\",\"rate\":\"40\",\"liked\":\"40\"}},{\"9\":{\"PID\":\"86942\",\"PName\":null,\"PAddr\":\"Use code at checkout to get 50% discount on all purchases.\",\"PDistrict\":\"50% Off All Orders\",\"PCity\":\"dream50\",\"PImage\":\"https:\\\/\\\/s3-eu-west-1.amazonaws.com\\\/getdeal\\\/images\\\/logos\\\/tracy-porter.jpg\",\"PPhone\":\"blog.tracyporter.com\",\"PPrice\":\"dream50\",\"PLat\":\"http:\\\/\\\/www.getdeal.com\\\/gutschein-frameset\\\/?id=86942\",\"PLong\":\"http:\\\/\\\/www.getdeal.com\\\/gutschein-frameset\\\/?id=86942\",\"TID\":\"Art\",\"UID\":\"discount\",\"countlike\":\"%\",\"distance\":\"50\",\"sao\":\"50\",\"rate\":\"50\",\"liked\":\"50\"}}],\"page\":\"1\"}";
NSData *responseData = [NSData dataWithData:[response dataUsingEncoding:NSUTF8StringEncoding]];
NSDictionary *jsonResponse = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:nil];
NSLog(@"%@",[jsonResponse allKeys]);
日志中的响应
2013-09-13 11:40:33.750 JsonExample [65429:c07]( 数据, 页 )
我将JSON响应视为字典而不是列表。
答案 2 :(得分:0)
您的JSON文本不是以数组或对象开头,而是选项允许未设置片段。
答案 3 :(得分:-1)
尝试使用以下代码:
NSString *str=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
NSMutableDictionary *dicRes = [str JSONValue];