Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

ios

iphone

ios7

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.}

like image 328
LvN Avatar asked Jan 30 '14 09:01

LvN


3 Answers

It usually is because of some warning message throwing out from your server without putting it in the response array. For example in PHP, some "warning messages" are not caught in your array so that when you finally use "echo json_encode($RESPONSE_ARR)," it is not a JSON format.

like image 38
Exile3daime Avatar answered Nov 09 '22 16:11

Exile3daime


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?

like image 199
Dave Burke Avatar answered Nov 09 '22 16:11

Dave Burke


The JSON that you posted looks Ok. If that is what your iOS application received, it would get parsed. Even if it wasn't Ok, you wouldn't get this error message. JSON must start with '[' if it is an array, and '{' if it is a dictionary like yours, and everything else you get the error message that you got. So even if you sent '[934knsdf239][@@@' you wouldn't get this error message, because the data starts with the [ indicating an array.

You need to debug this in the iOS application. First convert the data to a string and print that and check it. If the string looks alright then print the data itself - sometimes people manage to add 0 bytes or control characters, or two byte order markers or something similar which are invisible in the string but are not legal JSON.

The option NSJSONReadingAllowFragments allows JSON that consists just of a string, number, boolean or null value. Don't use this unless you want to be able to process one of these on its own.

like image 1
gnasher729 Avatar answered Nov 09 '22 18:11

gnasher729