0

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"
mystic cola
  • 1,465
  • 1
  • 21
  • 39
  • 1
    How can we help without seeing the JSON data? – Martin R Sep 13 '13 at 08:52
  • NSLog(@"placeJson: %@", placeJson);???????? – Prasad G Sep 13 '13 at 08:57
  • Where is you JSON Data? – thatzprem Sep 13 '13 at 09:01
  • @mysticcola: If I open that URL in my browser, I see "0LziY1YD4BZ8a6eQtemf/dmfyoz/AfLbDlAxcZ8FY..." and no JSON. - If you are "running on a tight timeframe" as you said, then you should not waste our time by providing incomplete and wrong information. – Martin R Sep 13 '13 at 09:07
  • Sorry. Just stressed that's all. It's getcoupons.php not getcoupons3.php – mystic cola Sep 13 '13 at 09:20
  • http://mobapps.getdeal.me/getcoupons.php?category=art – mystic cola Sep 13 '13 at 09:20
  • 1
    possible duplicate of [How can you deserialize an escaped JSON string with NSJSONSerialization?](http://stackoverflow.com/questions/16948427/how-can-you-deserialize-an-escaped-json-string-with-nsjsonserialization) – Martin R Sep 13 '13 at 09:24
  • 1
    Your JSON response is "nested JSON". For whatever reason, the JSON data is encoded once more as a JSON string. See the "possible duplicate" for a similar question and a solution. - Or tell the service provider to send proper JSON data. – Martin R Sep 13 '13 at 09:26
  • What happened was we needed the nata to fit a particular format. Anyway... this is the error I get when I try the possible dupe: No visible @interface for 'NSArray' declares the selector 'dataUsingEncoding:' – mystic cola Sep 13 '13 at 09:45
  • Your JSON Object: (null) Or Error is: Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0x756f070 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.} – thatzprem Sep 13 '13 at 09:45
  • @mysticcola: Is http://mobapps.getdeal.me/getcoupons.php?category=art really the URL that you loaded into `jsonResponse`? Because then `NSArray* placeJson = [NSJSONSerialization JSONObjectWithData:jsonResponse ...]` would be `nil`. – Martin R Sep 13 '13 at 09:50
  • posted response above. – mystic cola Sep 13 '13 at 10:07
  • getcoupons3 is just encrypted getcoupons. – mystic cola Sep 13 '13 at 10:10
  • @mysticcola you are not clear what u want i check below answer some of right answer :( – Rushabh Sep 13 '13 at 10:16

4 Answers4

2

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;
}
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • Almost there! Page now returns '1' then NSMutableArray * placeDetail = [[NSMutableArray alloc] init]; NSDictionary * pdJson = [dict objectForKey:@"detail"]; NSLog(@"%d", [pdJson count]);returns: unrecognized selector sent to instance 0xa8513d0 – mystic cola Sep 13 '13 at 10:56
  • Any idea how to fix the selector? – mystic cola Sep 13 '13 at 11:00
  • Example of working json: http://mobapps.getdeal.me/data.php?t=QA can't really see the difference – mystic cola Sep 13 '13 at 11:18
  • @mysticcola: I cannot see any key `"detail"` in the JSON, so what is `[dict objectForKey:@"detail"]`? - Anyway, you should check what types of objects you get (string, array, dictionary) and then apply the appropriate methods. – Martin R Sep 13 '13 at 11:20
  • Thanks Martin. Thanks everyone that helped me. Really appreciate it. – mystic cola Sep 13 '13 at 12:30
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]);

Response in Log

2013-09-13 11:40:33.750 JsonExample[65429:c07] ( data, page )

I am treating the JSON response as dictionary than list.

slysid
  • 5,236
  • 7
  • 36
  • 59
0

Your JSON text did not start with array or object and option to allow fragments not set.

thatzprem
  • 4,697
  • 1
  • 33
  • 41
-1

Try use with below code :

NSString *str=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
NSMutableDictionary *dicRes = [str JSONValue];
Nirmalsinh Rathod
  • 5,079
  • 4
  • 26
  • 56