0

I have the following code and it is working to an extent :

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    NSString *strURL = [NSString stringWithFormat:@"http://localhost:8888/service.php"];
    NSURL *url = [NSURL URLWithString:strURL];

    NSData * data = [NSData dataWithContentsOfURL:url];
    NSError * error;
    NSMutableDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];

    AdObject *someAdObject = [[AdObject alloc] init];



    //NSLog(@"%@", json);
    self.detailLabel.text = self.tempString;

}

Now when the commented out NSLog actually prints the JSON dictionary, I get :

        {
        brand = "";
        category = Games;
        country = "Japan";
        "discount_rate" = 50;
        duration = 5;
        id = 1;
        "issue_date" = "2014-04-07";
        location = "Heishi Mall";
        title = "Gamestory videogames sales!";
        user = "";
    }
)

I created an Ad object which has properties such as title, location, country, etc (as reflected above). I would like to access the JSON above and store value in object variables.

Irfan
  • 4,301
  • 6
  • 29
  • 46
spacemonkey
  • 2,530
  • 6
  • 23
  • 27

2 Answers2

3

You can access that values :-

 for(NSDictionary *item in json) {                  
    NSLog(@"%@",[item valueForKey:@"key"]);
   someAdObject.key  = [item valueForKey:@"key"];                 
 }
Priyank Gandhi
  • 1,257
  • 1
  • 16
  • 29
1

Try this and review your json also

AdObject *someAdObject = nil;
for(NSDictionary *item in json) {
    someAdObject = [[AdObject alloc] init];
    someAdObject.category = [item valueForKey@"category"]; 
    someAdObject.country = [item valueForKey@"country"]; 
    someAdObject.discount_rate = [item valueForKey@"discount_rate"]; 
    someAdObject.duration = [item valueForKey@"duration"]; 
    //and same all of your required object properties
}
Buntylm
  • 7,345
  • 1
  • 31
  • 51