Mac
Mac

Reputation: 111

Missing quotes around keys in AFJSONRequestOperation result?

Been stuck all day with this and reach out to get some help solving this asap. Any help would be great. Thank's :-)

The missing quotes

Got this AFJSONRequestOperation request from the web and it works great exept for one crucial fault: some keys are missing the quotes around there values = cant use them! There's obviusly some parsing going on in the AFNetworking background since the values come back i alphabetical order + "trimmed" of some of the qoutes on some values. But I just want the webdata "as is"...

The format should look like this; theKey = "theValue";

But sometimes it looks like this; theKey = theValue;     // Note the missing quotes around the value

The web output json:

I've also checked that my json is correct formatted coming from the server, the output looks like this;

{"results":[{"ID":"3","Row_state":"Visible","CustName":"Customer Name","CustReferens":"Customer Referens","CustReferensMobil":"123456mob","CustNotes":"Customer Notes","Tel":"123456tel","Fax":"123456fax","Web":"www.example.se","Mail":"[email protected]","Street":"The road 5","Zip":"11122","City":"Stockholm","Products":"","Images":"","Logo":"myLogo.jpg","Text":"A lot of text goese here...","Seller":"","Favorite":"YES"},

In the app I get this result;

results =     (
            {
        City = Stockholm;          // Note the missing quotes around the value
        CustName = "Customer Name";
        CustNotes = "Customer Notes";
        CustReferens = "Customer Referens";
        CustReferensMobil = 123456mob;     // Note the missing quotes around the value
        Favorite = YES;             // Note the missing quotes around the value
        Fax = 123456fax;          // Note the missing quotes around the value
        ID = 3;             // Note the missing quotes around the value
        Images = "";
        Logo = "myLogo.jpg";
        Mail = "[email protected]";
        Products = "";
        "Row_state" = Visible;      // Now key got quotes + Note the missing quotes around the value
        Seller = "";
        Street = "The road 5";
        Tel = 123456tel;         // Note the missing quotes around the value
        Text = "A lot of text goese here...";
        Web = "www.example.se";
        Zip = 11122;       // Note the missing quotes around the value
    },

Instead of this expected result (all keys with quotes around the value);

results =     (
            {
        City = "Stockholm";
        CustName = "Customer Name";
        CustNotes = "Customer Notes";
        CustReferens = "Customer Referens";
        CustReferensMobil = "123456mob";
        Favorite = "YES";
        Fax = "123456fax";
        ID = "3";
        Images = "";
        Logo = "myLogo.jpg";
        Mail = "[email protected]";
        Products = "";
        "Row_state" = Visible;
        Seller = "";
        Street = "The road 5";
        Tel = "123456tel";
        Text = "A lot of text goese here...";
        Web = "www.example.se";
        Zip = "11122";
    },

My code for this;

NSURL *myUrl = [NSURL URLWithString:@"http://example.se/api.lasso"];
NSURLRequest *request = [NSURLRequest requestWithURL:myUrl];
AFJSONRequestOperation *operation;
operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request
success:^(NSURLRequest *req, NSHTTPURLResponse *responce, id jsonObject) {
                                            NSLog(@"JSON Responce ÅF: %@",jsonObject);
                                            // Add result (an array from JSON) to NSMutableArray
                                            _restFeed = [jsonObject objectForKey:@"results"];
                                            NSLog(@"RestA-OListTVC > ViewDidload > AFN ÅF: _restFeed count = %i",[_restFeed count]);
                                            // Reload table with new data
                                            [self.tableView reloadData];
                                            }
failure:^(NSURLRequest *req, NSHTTPURLResponse *responce, NSError *error, id jsonObject) {
            NSLog(@"Recieved an HTTP %d", responce.statusCode);
            NSLog(@"The error was: %@",error);
            }];
[operation start];

Upvotes: 2

Views: 505

Answers (1)

mattt
mattt

Reputation: 19544

All JSON keys have quotes around them--that's part of the specification. However, when turned into an NSDictionary, their keys lose the quotes and just become NSString objects.

JSON {"a": 1} => Objective-C @{@"a" : @(1)}

NSLog will strip the quotes around strings that don't contain whitespace. Don't use the output of NSLog as the literal representation of what you're getting back. Just use the values as expected, with valueForKeyPath: and it should all work just fine.

Upvotes: 3

Related Questions