5

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);
Community
  • 1
  • 1
Mukesh
  • 777
  • 7
  • 20

4 Answers4

1

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];
0

You have array wrappped by another array. So, use this direction:

NSDictionary *json=//..initialized
    NSArray *pages = [json valueForKey@"pages"];
    NSDictionary *page = [pages objectAtIndex:0];
    NSArray *ar1 = [page valueForKey@"cover_url"];
            NSArray *ar2 = [ar1 objectAtIndex:0];
            NSDictionary *elem = [ar2 objectAtIndex:0];
            NSString *value = [elem valueForKey@"url"];
Vyacheslav
  • 26,359
  • 19
  • 112
  • 194
0

I would suggest using Mantel or JSONModel libraries for parsing objects and having decent DTOs.

Ashraf Tawfeeq
  • 3,036
  • 1
  • 20
  • 34
0

When you deal with something like this, i suggest putting the complete json in http://jsonviewer.stack.hu/ so you can see the correct structure without getting confused.

Then it's just a matter of digging. When you see { }, you add a dictionary, when you see [ ], you add an array, until you reach your url object :)

I'm not writing the code because it is pretty trivial, just a mix of objectForKeys for dictionaries and objectAtIndex for arrays.

The last layers are just objects, so they're handled like any other object.

If you're confused about json, I suggest you try giving your json-parser a simple json (you hard core it yourself just above, its really just for testing).

Small advice :

Give it a simple array of 1 object, then 2, then put the array in a dict, then two, etc. and you keep making the json more complex until you really understand how it works. Then you'll eventually have a fake json just like your real one, and you can remove the fake and use the real one :)

Other advice :

There are many json parsing libraries that let you create the object model, where you can create (for example) a Page object that has an ID, a title, a cover URl, etc. that matches the JSON structure, and then you just tell the parser " make that JSON a Page!" and voilà, you have a Page. I don't know any of those json libraries in ios, but people will surely link it here, so try it out ! They're super easy to use and make json parsing really straightforward. And also, you don't have to map everything manually like you're doing ;)

Gil Sand
  • 5,802
  • 5
  • 36
  • 78