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

Config.h

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

4 に答える 4

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 に答える