Malloc
Malloc

Reputation: 16296

NSDebugDescription = "JSON text did not start with array or object and option to allow fragments not set.";

I am using AFJSONRequestOperation to request a server and parse the returned JSON response, but while parsing, I got this error:

NSDebugDescription = "JSON text did not start with array or object and option to allow fragments not set.";

I checked the API and it's returning JSON data:

header('Content-type: text/json');
$arr[] = array("Message" => "update succeeded");
echo '{"Result":'.json_encode($arr).'}';

Any idea how to fix that?

EDIT

I tried to call the API from browser and include the request in the url, and so I got a valid JSON response:

{"Result":[{"Message":"update succeeded"}]}

Upvotes: 24

Views: 5321

Answers (6)

yergo
yergo

Reputation: 4980

First of all, your API is returning improper Content-Type. Proper content type for JSON data is application/json. This may be conflicting while using third-party libraries.

Secondary, you should not produce json string "by hand". Altogether API should be modified to face:

header('Content-type: application/json');
$arr[] = array("Message" => "update succeeded");
echo json_encode(array('Result' => $arr));

Last bat not least. There is one more thing in charge possible: you might have BOM characters at very beginning of your api PHP script. Those are whitespace, so you may not see them in browser. Please, ensure that your PHP files are encoded without BOM.

Upvotes: 0

user1479883
user1479883

Reputation: 51

If you need read fragment json you can use option .allowFragments like this:

JSONSerialization.jsonObject(with: someData, options: .allowFragments)

Upvotes: 1

Mogens TrasherDK
Mogens TrasherDK

Reputation: 680

To make a valid json response, your code should look something like this:

$response = array(
    "Result" => array(
        "Message" => "update succeeded"
    )
)

echo json_encode($response);

Upvotes: 1

Gurjinder Singh
Gurjinder Singh

Reputation: 10329

Check you have added /api/ before your base url of API like

http:// someurl / yourBasrUrl /api/apiName

Upvotes: 1

Vishal Deshai
Vishal Deshai

Reputation: 21

Please use acceptable content type. in your webservice that should be only plain text.

here is my swift code and fixed:

    let manager = AFHTTPRequestOperationManager()

    manager.requestSerializer=AFJSONRequestSerializer()
    manager.responseSerializer = AFHTTPResponseSerializer();

    manager.GET(

        baseURL + (webServiceType as String) + secureParam,
        parameters:[:],

        success:
        { (operation: AFHTTPRequestOperation!,
            responseObject: AnyObject!) in
            completion(obj: responseObject)
        },
        failure:
        { (operation: AFHTTPRequestOperation!,
            error: NSError!) in
            completion(obj: nil)
    })

Upvotes: 1

ajacian81
ajacian81

Reputation: 7599

First thing, json_encode the entire object rather than breaking into it pieces.

Secondly, unless $arr contains multiple elements (not clear from example above), it should be initalized as thus:

$arr = array("Message" => "update succeeded");

I'm still not certain what else could be the issue here. You should echo out what your app is receiving and that should indicate the issue.

Upvotes: 1

Related Questions