Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS JSON Parsing, array with multiple array

I have a JSON array with multiple object and I don't know how do I grab the "url" tag as an NSArray or a NSDictionary and show that image url in Tableview. I can't change the JSON data format.How should I do this?

Here is what the JSON response looks like:

{
"meta": {
    "total_count": 10
},
"pages": [
    {
        "id": 7,
        "meta": {
            "type": "dashboard.NewsEvents",
            "detail_url": "http://suno.to/api/v1/pages/7/"
        },
        "title": "NoEvent",
        "created_at": "2016-03-06T10:42:19.646000Z",
        "cover_url": [
            [
                {
                    "url": "/media/images/Maha_Shivratri2.original.jpg",
                    "title": "Maha Shivratri2.jpg"
                },
                {
                    "url": "/media/images/Maha_Shivratri1.original.jpg",
                    "title": "Maha Shivratri1.jpg"
                }
            ],
            [
                {
                    "url": "/media/images/Celebrations.original.jpg",
                    "title": "Celebrations.jpg"
                },
                {
                    "url": "/media/images/Crew.original.jpg",
                    "title": "Crew.jpg"
                },
                {
                    "url": "/media/images/World_record.original.jpg",
                    "title": "World record.jpg"
                },
                {
                    "url": "/media/images/AI_pilots.original.jpg",
                    "title": "AI pilots.jpg"
                }
            ],
            [
                {
                    "url": "/media/images/CbVv-VbWEAAmwv_.original.jpg",
                    "title": "DAL SWARAJ YATRA"
                },
                {
                    "url": "/media/images/CbVv_-TWwAE7RjM.original.jpg",
                    "title": "DAL SWARAJ YATRA"
                },
                {
                    "url": "/media/images/CbVv_SmXIAALQP8.original.jpg",
                    "title": "DAL SWARAJ YATRA"
                },
                {
                    "url": "/media/images/CahEc--UkAArc_z.original.jpg",
                    "title": "DAL SWARAJ YATRA"
                }
            ]
        ]
    },
    {
        "id": 2530,
        "meta": {
            "type": "dashboard.NewsEvents",
            "detail_url": "http://suno.to/api/v1/pages/2530/"
        },
        "title": "World Culture Festival",
        "created_at": "2016-03-12T06:59:21.023000Z",
        "cover_url": [
            [
                {
                    "url": "/media/images/Security.original.jpg",
                    "title": "Security check"
                }
            ],
            [
                {
                    "url": "/media/images/Elephant_statues.original.jpg",
                    "title": "Elephant"
                }
            ],
            [
                {
                    "url": "/media/images/6.original.jpg",
                    "title": "Stage"
                },
                {
                    "url": "/media/images/4.original.jpg",
                    "title": "Stage"
                }
            ]
        ]
    },
    {
        "id": 2675,
        "meta": {
            "type": "dashboard.NewsEvents",
            "detail_url": "http://suno.to/api/v1/pages/2675/"
        },
        "title": "Holi in Barsana",
        "created_at": "2016-03-17T12:35:09.308000Z",
        "cover_url": [
            [
                {
                    "url": "/media/images/Brajwasi_playing_holi_.original.jpg",
                    "title": "Holi in Barsana"
                },
                {
                    "url": "/media/images/dancing_.original.jpg",
                    "title": "Holi in Barsana"
                },
                {
                    "url": "/media/images/holi.._.original.jpg",
                    "title": "Holi in Barsana"
                },
                {
                    "url": "/media/images/holi..._.original.jpg",
                    "title": "Holi in Barsana"
                }
            ],
            [
                {
                    "url": "/media/images/Lathmar_holi_19_n54f7LJ.original.jpg",
                    "title": "Lathmar Holi in Barsana"
                }
            ],
            [
                {
                    "url": "/media/images/Lathmar_holi_17.original.jpg",
                    "title": "Lathmar Holi in Barsana"
                },
                {
                    "url": "/media/images/Lathmar_holi_20.original.jpg",
                    "title": "Lathmar Holi in Barsana"
                }
            ]
        ]
    },

I'm using this code to get the "url" array. Plz correct me ?

 NSArray *imageUrlArray = [[self.jsonData    objectAtIndex:indexPath.row]objectForKey:@"cover_url"];
  NSLog(@"IMAGE URL ARRAY:%@",imageUrlArray);


  NSString *imageUrl = [imageUrlArray valueForKey:@"url"];
  NSLog(@"IMAGE URL:%@",imageUrl);
like image 433
Mukesh Avatar asked Nov 09 '22 13:11

Mukesh


1 Answers

To view the JSON structure - http://jsonviewer.stack.hu/

NSMutableArray* imageurlArray = [NSMutableArray new];
NSArray* jsonArray = jsonData[@"pages"];

for (int i = 0; i<[jsonArray count]; i++) {
    NSArray* coverUrlArray = jsonArray[i][@"cover_url"];
    for (int t = 0; t< [coverUrlArray count]; t++) {
        NSArray* UrlArray = coverUrlArray[t];
        for (int x = 0; x<[UrlArray count]; x++) {
            [imageurlArray addObject:UrlArray[x][@"url"]];
        }
    }
}
NSLog(@"imageurlArray: %@", imageurlArray);

//imageurlArray contains all url
//In cell for row at indexpath --> use imageurlArray[indexPath.row];
like image 75
Gowrie Sammandhamoorthy Avatar answered Nov 15 '22 04:11

Gowrie Sammandhamoorthy