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

ios, ios7, iphone

Solution

I ran into the same error when consuming a feed from a php page. Just as you encountered, the resulting json string passed a visual inspection, but would fail to serialize. My suspicion was that there was a hidden character somewhere the feed, so I converted each character to its unicode decimal equivalent and examined the results:

NSString *feedStr = [[NSString alloc] initWithData:feedData encoding:NSUTF8StringEncoding];
for(int i=0; i<[feedStr length]; ++i)
{
    unichar c = [feedStr characterAtIndex:i];
    NSLog(@"decimal char %d", c);
}

I found that before the first character and after the last was the character #65279. After a quick google search I found What is this char? 65279, where this was identified as a byte order mark.

In my case, I was able to fix this at the source by opening and saving all included php files, using a text editor that provided an option to use the "Encode in UTF-8 without BOM" encoding. For more info on the php side, see How to avoid echoing character 65279 in php?

Problem

I'm sending this json response from server for a request to my IOS 7 app. ``` { "root": { "success": "1", "message": "Successfully retrieved data.", "data": { "records": [ { "receipt_key": "xxxxxxxx", "receipt_id": "xxxxxxxx", "store_name": "xxxxxx", "amount": "xxxx", "date_purchase": "xxxxxxxx", "is_processed": "x", "created_on": "xxxxxxxx", "modified_on": "xxxxxxxx", "modified_on_millis": "xxxxxxxx", "user_folder": "xxxxxxxx", "category_id": "xxxxxxxx", "is_deleted": "x", "currency_id": "xxxxxxxx" } ] } } } ``` I use the following code for parsing the above json to NSDictionary object. ``` NSMutableDictionary *json=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error]; ``` But I'm getting this error on the above code. 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=0x8a8a700 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}

Original source

Related problems