JSON text did not start with an array or object, and the option to not set fragments

I am sending this json response from the server to request to my iOS 7 application.

{ "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 am using the following code to parse the above json into an NSDictionary object.

  NSMutableDictionary *json=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error]; 

But I get this error on the above code.

Domain error = NSCocoaErrorDomain Code = 3840 "Operation could not be completed. (Cocoa error 3840.)" (JSON text did not start with an array or an object and a parameter that allows not creating fragments.) UserInfo = 0x8a8a700 {NSDebugDescription = JSON text did not start with array or object and the ability to allow fragments.}

+12
ios iphone ios7
Jan 30 '14 at 9:27
source share
5 answers

I encountered the same error when using a feed from a php page. Just as you came across, the resulting json string went through a visual inspection, but could not be serialized. My suspicion was that there was a hidden character somewhere, so I converted each character to its decimal equivalent in unicode and looked at 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 search on Google, I found What is this char? 65279 , where it was defined as a byte order of bytes .

In my case, I was able to fix this in the source by opening and saving all the included php files using a text editor that made it possible to use the encoding "Encode in UTF-8 without specification". For more information about php side, see How to avoid echo symbol 65279 in php?

+10
Aug 13 '14 at 3:27
source share

This is usually due to the fact that a warning is issued from your server without placing it in the response array. For example, in PHP, some β€œwarning messages” do not get into your array, so when you finally use β€œecho json_encode ($ RESPONSE_ARR)”, this is not JSON format.

+2
Jul 31 '16 at 4:13
source share

The JSON you posted looks good. If this is what was received by your iOS application, it will be disassembled. Even if this is not the case, you will not receive this error message. JSON should start with '[' if it is an array, and '{' if it is a dictionary like yours, and everything else you get the error message you received. Therefore, even if you sent "[934knsdf239] [@@@", you would not receive this error message because the data starts with [points to an array.

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

The NSJSONReadingAllowFragments option allows JSON consisting of only a string, number, boolean, or null value. Do not use this if you do not want to process one of them yourself.

+1
Jul 31 '16 at 5:54
source share

I ran into the same problem. But I found that Url I, being sent as a parameter to the server, was wrong. There was a mistake of one character. For example, I sent the following URL

 URL = https://somerUrl/api/v2/&venues/search?client_id=jkdasljf3242jka-fsdf-fadsfasd&lat=40.712488&long=-74.006277&distance=25 

The mistake was superfluous and a symbol in front of the sites that created the problems. So I deleted, and the symbol also found a job for me. Therefore, make sure that you send the correct parameter to the server.

0
Dec 07 '17 at 10:52
source share

The problem is due to parsing the answer. You are trying to de-serialize a JSON response (which MUST be contained in an NSArray or NSDictionary ), however your answer is none of the above (most likely a simple string).

You can try to print the answer of your server. Use the code in your catch block. and server-side error detection or not.

Your server data does not match the JSON format, and then print your server data and verify that the server data is valid or not.

 URLSession.shared.dataTask(with: url) { (data, response, error) in if let jsonData = data { do { let parsedData = try JSONSerialization.jsonObject(with: jsonData, options: .mutableLeaves) as! [String: AnyObject] } catch let err{ print("\n\n===========Error===========") print("Error Code: \(error!._code)") print("Error Messsage: \(error!.localizedDescription)") if let data = data, let str = String(data: data, encoding: String.Encoding.utf8){ print("Server Error: " + str) } debugPrint(error) print("===========================\n\n") debugPrint(err) } } else { debugPrint(error as Any) } }.resume() 
0
Apr 05 '18 at 12:01
source share



All Articles