1

The code:

NSURL * url=[NSURL URLWithString:@"alamghareeb.com/mobileData.ashx?catid=0&No=10"]; 
NSData * data=[NSData dataWithContentsOfURL:url]; 
NSError * error; 
NSMutableDictionary * json = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingMutableContainers error: &error];
NSMutableArray * referanceArray=[[NSMutableArray alloc]init];
NSMutableArray * periodArray=[[NSMutableArray alloc]init]; 
NSArray * responseArr = [NSArray arrayWithArray:json[@"item"]];

for (NSDictionary * dict in responseArr) { 
    [referanceArray addObject:[dict objectForKey:@"created"]]; 
} 

The JSON looks like:

{ 
  "item" = [ 


  {
  "id" : "2292",

  "created" : "10/01/2015 10:21:18 ص",

  "title" : "الإمارات: ملابس رياضية فاخرة لتحسين أداء الإبل في السباقات ", 

  "image_url" : "http://alamghareeb.com/Photos/L63556482078696289044.jpg",

  "image_caption" : ""

   },

  {

  "id" : "2291",

  "created" : "10/01/2015 09:28:11 ص",

  "title" : "طبيبة تجميل 'مزورة' تحقن مرضاها بالغراء والاسمنت",

  "image_url" : "http://alamghareeb.com/Photos/L63556478891290039015.jpg",

  "image_caption" : ""
  } 
 ]
}
Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • 1
    thanks for posting on the json but can you tell us what you have done to parse this your self? – MZaragoza Jan 11 '15 at 15:50
  • I would recommend that you use a JSON parser. – Hot Licks Jan 11 '15 at 15:53
  • NSURL * url=[NSURL URLWithString:@"http://alamghareeb.com/mobileData.ashx?catid=0&No=10"]; NSData * data=[NSData dataWithContentsOfURL:url]; NSError * error; NSMutableDictionary * json = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingMutableContainers error: &error]; NSMutableArray * referanceArray=[[NSMutableArray alloc]init]; NSMutableArray * periodArray=[[NSMutableArray alloc]init]; NSArray * responseArr = [NSArray arrayWithArray:json[@"item"]]; – sojoud ahmad Jan 11 '15 at 15:54
  • for(NSDictionary * dict in responseArr) { [referanceArray addObject:[dict objectForKey:@"created"]]; } @MZaragoza – sojoud ahmad Jan 11 '15 at 15:55
  • 1
    @sojoudahmad You need to put that code in your question, and not as comments here – Peter M Jan 11 '15 at 15:58
  • 1
    By the way, you generally shouldn't use mutable objects unless you really need mutable objects. For example, I'd change the declaration of the `json` object as follows: `NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error: &error];` – Rob Jan 11 '15 at 17:27
  • 1
    Also, it's inadvisable to use `dataWithContentsOfURL` (a synchronous JSON function). I'd use `sendAsynchronousRequest` or other asynchronous method. – Rob Jan 11 '15 at 17:27
  • 1
    Hint: There is an `error:` parm on `JSONObjectWithData:`. Use it. – Hot Licks Jan 11 '15 at 19:20
  • And first of all: use properly formatted JSON to start with... the response you are getting from alamghareeb.com is faulty. – Rok Jarc Jan 11 '15 at 19:44

2 Answers2

3

This JSON is not valid. Did you build it manually? The "item" = ... should be "item" : ....

In the future, you can run your JSON through http://jsonlint.com to verify any issues. Likewise, you should add error checking in your Objective-C code, e.g.

NSMutableDictionary *json = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingMutableContainers error: &error];
if (!json) {
    NSLog(@"JSON parsing error: %@", error);
}

I'd also suggest changing your server code to not build JSON manually, but take advantage of JSON functions (e.g. if PHP, build associative arrays and then generate JSON output using the json_encode function).


So, once you fix the JSON error, to parse the results might look like:

NSError *error;
NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
if (!dictionary) {
    NSLog(@"NSJSONSerialization error: %@", error);
    return;
}

NSArray *items = dictionary[@"item"];

for (NSDictionary *item in items) {
    NSString *identifier = item[@"id"];
    NSString *created    = item[@"created"];
    NSString *title      = item[@"title"];
    NSString *urlString  = item[@"image_url"];
    NSString *caption    = item[@"image_caption"];

    NSLog(@"identifier = %@", identifier);
    NSLog(@"created = %@", created);
    NSLog(@"title = %@", title);
    NSLog(@"urlString = %@", urlString);
    NSLog(@"caption = %@", caption);
}
Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • OP actually posted part of the endpoint result: http://alamghareeb.com/mobileData.ashx?catid=0&No=10 at least part of the problem seems to be on the server side – Rok Jarc Jan 11 '15 at 18:46
0

You can use NSJSONSerialization. No need to import any class.

NSDictionary *result = [NSJSONSerialization JSONObjectWithData:[strResult dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];

or use NSData directly as

NSDictionary *result = [NSJSONSerialization JSONObjectWithData:theJSONDataFromTheRequest options:0 error:nil];

Source : Stackoveflow question

Community
  • 1
  • 1
Amit Singh
  • 2,698
  • 21
  • 49