0

I have the following JSON data:

[{"id":"value","first_name":"value","last_name":"value"},
 {"id":"value","first_name":"value","last_name":"value"},
 {"id":"value","first_name":"","last_name":""}]

Then I implement my code :

NSError *err;
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://abcd.com/index.php?r=WS/Employee"]];
NSData *dataFromUrl = [NSData dataWithContentsOfURL:url];
NSMutableArray *array = [NSJSONSerialization JSONObjectWithData:dataFromUrl options: NSJSONReadingMutableContainers error:&err];

if (err)
    NSLog(@"JSONObjectWithData error: %@", err);

For which, I get the following error message:

JSONObjectWithData error: 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=0x9c900f0 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}

Please help me to get those data.

5
  • there's something else coming with this json in response of your service.. Commented Aug 13, 2014 at 8:25
  • How is the JSON created? Is it encoded using a valid JSON encoding? UTF-8, UTF-16LE, UTF-16BE, UTF-32LE or UTF-32BE Commented Aug 13, 2014 at 8:30
  • I use PHP to create it. echo "<meta charset=\"utf-8\">"; echo json_encode($r); Commented Aug 13, 2014 at 8:42
  • Try removing the meta-data tag. You should use the http header content-type instead. Commented Aug 13, 2014 at 9:01
  • I can fix this with "header('Content-type: application/json');" Commented Aug 13, 2014 at 9:04

4 Answers 4

1

I think u should put something more in request header. Try convert NSData to NSString and look what is comming for you

Sign up to request clarification or add additional context in comments.

2 Comments

NSLog(@"%@",[[NSString alloc]initWithData:dataFromUrl encoding:NSUTF8StringEncoding]); And I get collect messages as I post JSON data above
try to use this library github.com/icanzilb/JSONModel They have their own built-in HTTP client.
1

I fix this by replace request header in my PHP file from

echo "<meta charset=\"utf-8\">";

to

 ob_start();
 header('Content-type: application/json');

Comments

0

try this code....

NSURL * url =[NSURL URLWithString:getDataURL];
NSData * data=[NSData dataWithContentsOfURL:url];

json =[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];

Comments

0
NSError *err;
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://abcd.com/index.php?r=WS/Employee"]];
NSData *dataFromUrl = [NSData dataWithContentsOfURL:url];
NSMutableArray *array = [NSJSONSerialization JSONObjectWithData:dataFromUrl options:     NSJSONReadingMutableContainers error:&err];

if (err)
   NSLog(@"JSONObjectWithData error: %@", err);

Just Change your line

NSMutableArray *array = [NSJSONSerialization JSONObjectWithData:dataFromUrl options:     NSJSONReadingMutableContainers error:&err];

with

NSJSONSerialization *jsonData = [NSJSONSerialization JSONObjectWithData:dataFromUrl options:     NSJSONReadingMutableContainers error:&err];
NSMutableArray *array = [NSMutableArray arrayWithArray:(NSArray *)jsonData];
NSLog(@"Success:%@", array);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.