mreynol
mreynol

Reputation: 409

not parsing PHP JSON

I am not sure if i have a problem with my JSON or with my code in xcode, but i cannot get xcode to parse my JSON. My JSON is valid however I cannot get it to parse. Anyone see what i am doing wrong?

Here is my PHP for generating JSON:

 $channel['items']['post']['category_name'][$category_name]['details'][] = array(
    'title' => $title,
    'description' => $description,
    'category_id' => $category_id,

    );
}   
$channels = array($channel);
$json = json_encode($channel);
header('Content-type: application/json');
echo $json;

Which gives JSON like this:

{

"items": {

    "post": {

        "category_name": {

            "Baby": {

                "details": [

                    {

                        "title": "trying with category id again",

                        "price": "3344.55",

                          },



                    {

                        "title": "this is sending with description ",

                        "price": "900000.00",

                    }

                ]

            },

            “Other Baby Stuff": {

                "details": [

                    {

                        "title": "putting in title",

                        "price": "3000.99",

Here is my code in xcode to parse (i want my section headers the category name like "Baby", "Other Baby Stuff", etc:

- (void)fetchedData:(NSData *)responseData {
//parse out the json data
NSError* error;

NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];

dispatch_async(dispatch_get_main_queue(), ^{
    [m_tableView reloadData];

});
_buyCategory =  json[@"items"][@"post"][@"category_name"];//THIS IS PARSING JUST FINE NOW
NSLog(@"Items: %@", _buyCategory);
NSLog(@"Array Count: %u", [_buyCategory count]);

}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if (appDelegate.bLoadBuy == 1) {
    NSString *section = [[_buyCategory allKeys] objectAtIndex:0]; ///NOT RETURNING ANYTHING
    NSLog(@"category: %@", section); //I AM NOT EVEN GETTING NSLOG HERE

    return [NSString stringWithFormat:@"%@",section];];

And in my tableview cell (i want my details)

_buyTitle = [[tempResults objectForKey:@"details"] objectForKey:@"title"];

Upvotes: 0

Views: 145

Answers (1)

chuthan20
chuthan20

Reputation: 5409

NSDictionary *category = json[@"items"][@"post"][@"category_name"];

//section title
NSString *section = [[category allKeys] objectAtIndex:0]

//array of detail object..  so details[0][@"title"] should give you the first titme
NSArray *details = [category objectForKey:section]; 

Upvotes: 1

Related Questions