0

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

配置文件

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

4 回答 4

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]);

输出:

"{\"数据\":[[{\"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;
}
于 2013-09-13T10:11:42.500 回答
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 响应视为字典而不是列表。

于 2013-09-13T09:42:18.643 回答
0

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

于 2013-09-13T09:47:24.003 回答
-1

尝试使用以下代码:

NSString *str=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
NSMutableDictionary *dicRes = [str JSONValue];
于 2013-09-13T09:32:07.190 回答