Tejas Patel
Tejas Patel

Reputation: 870

How to get value from NSArray ios

I have got json response like following

Photo = ["\image1.jpg\","\image2.jpg\","\image3.jpg\"]

How can i get only name from this json response.

I want output like

Image1.jpg
Image2.jpg
Image3.jpg

Without [] and "".

Upvotes: 1

Views: 176

Answers (1)

Dinesh
Dinesh

Reputation: 1137

First of all, Your Photo String is not in proper format. The correct String is as follows:

NSString *jsonString = @"[\"\image1.jpg\",\"\image2.jpg\",\"\image3.jpg\"]";

Try out the below code to get image name from Your String:

    NSString *jsonString = @"[\"\image1.jpg\",\"\image2.jpg\",\"\image3.jpg\"]";
    NSData *data = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
        NSError *e = nil;
        NSArray *json = [NSJSONSerialization JSONObjectWithData: data options: NSJSONReadingMutableContainers error: &e];

        if (!json) {
            NSLog(@"Error parsing JSON: %@", e);
        } else {
            NSLog(@"Item: %@", json);
            for(NSString *item in json) {
                NSLog(@"Item: %@", item); 
            }
        }

Upvotes: 1

Related Questions