NSArray 返回字符串而不是字典

Posted

技术标签:

【中文标题】NSArray 返回字符串而不是字典【英文标题】:NSArray Returning String Instead of Dictionary 【发布时间】:2013-09-13 08:48:50 【问题描述】:

我已经设置了日志并检查了代码,并且 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"];

配置.h

#define placeLink @"http://mobapps.getdeal.me/getcoupons3.php"

【问题讨论】:

在没有看到 JSON 数据的情况下如何提供帮助? NSLog(@"placeJson: %@", placeJson);???????? JSON 数据在哪里? How can you deserialize an escaped JSON string with NSJSONSerialization?的可能重复 您的 JSON 响应是“嵌套 JSON”。无论出于何种原因,JSON 数据都会再次编码为 JSON 字符串。有关类似问题和解决方案,请参阅“可能重复”。 - 或者告诉服务提供商发送正确的 JSON 数据。 【参考方案1】:

我会试一试的。正如我在评论中所说,您的提供商发送“嵌套 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]);

输出:

"\"数据\":[[\"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);

输出:

数据 = ( ( PAddr = "在收尾部分选择艺术品可节省 20-85%。只需单击链接,然后单击页面底部的“收尾上的绘画”。无需代码,价格与标记相同。"; PCity = ""; ... 率 = 20; 骚= 20; ; ); 页 = 1;

【讨论】:

快到了!页面现在返回 '1' 然后 NSMutableArray * placeDetail = [[NSMutableArray alloc] init]; NSDictionary * pdJson = [dict objectForKey:@"detail"]; NSLog(@"%d", [pdJson count]);返回:无法识别的选择器发送到实例 0xa8513d0 知道如何修复选择器吗? 工作 json 示例:mobapps.getdeal.me/data.php?t=QA 看不出区别 @mysticcola:我在 JSON 中看不到任何键 "detail",那么 [dict objectForKey:@"detail"] 是什么? - 无论如何,你应该检查你得到什么类型的对象(字符串、数组、字典),然后应用适当的方法。 谢谢马丁。感谢所有帮助过我的人。真的很感激。【参考方案2】:
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 响应视为字典而不是列表。

【讨论】:

【参考方案3】:

您的 JSON 文本没有以数组或对象开头,并且允许未设置片段的选项。

【讨论】:

【参考方案4】:

尝试使用以下代码:

NSString *str=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
NSMutableDictionary *dicRes = [str JSONValue];

【讨论】:

是在我拥有的之前还是之后? NSData* jsonResponse = [aesResponse dataUsingEncoding:NSUTF8StringEncoding];在此代码之后使用我的代码并检查。 那么 NSSTRING/NSMutable 紧随其后? 是的。从 NSData 我们得到字符串,从字符串我们得到 NSDictionary。 未声明的标识符urlData(切换到jsonResponse?)没有可见的接口NSString声明JSONvalue

以上是关于NSArray 返回字符串而不是字典的主要内容,如果未能解决你的问题,请参考以下文章

使用石墨烯在 Graphql 中返回字典而不是字符串

Python Json加载()返回字符串而不是字典?

.get() 返回字母而不是字符串

fetchedResultsController fetchedObjects 返回一个 NSCFString 而不是 NSArray

iOS:有没有办法检查 NSArray 对象是不是包含某个字符?

使用 Array 而不是 NSArray 时出现错误“对成员下标的模糊引用”