1

I am having a problem displaying images from a JSON URL. I want to concatenate two strings to display images from the JSON URL, but I got an error in the console and I am not able to display the images.

JSON:

{
    "status": "200",
    "requestType": "bannerImages",
    "basePath": "http:\/\/192.168.0.33\/cartwebsite3\/",
    "bannerPath": "http:\/\/192.168.0.33\/cartwebsite3\/cdn-images\/banner\/",
    "response": {
        "data": [{
            "banner_id": "40",
            "banner_name": "14613036182de40e0d504f583cda7465979f958a98.jpg",
            "banner_link": "",
            "banner_blocked": "0",
            "banner_created": "admin",
            "created_on": "1461303618"
        }, {
            "banner_id": "39",
            "banner_name": "1461303615f99687dd719c4e8bc6a39e946c3d9ef7.jpg",
            "banner_link": "",
            "banner_blocked": "0",
            "banner_created": "admin",
            "created_on": "1461303615"
        }, {
            "banner_id": "38",
            "banner_name": "14613036094efdd2f969559e8b1c92e99f32ded48e.jpg",
            "banner_link": "",
            "banner_blocked": "0",
            "banner_created": "admin",
            "created_on": "1461303609"
        }]
    },
    "request": {
        "postData": [],
        "getData": {
            "type": "bannerImages",
            "result": "json"
        }
    }
}

Code:

 NSArray * arr1 = [json objectForKey: @ "bannerPath"];
    NSLog(@ "%@", arr1);
        NSMutableArray * arr2 = [json objectForKey: @ "basePath"];
        NSLog(@ "%@", arr2);
        [statusarray addObject: outstatus];
        [ban_path_arr addObject: arr1];
        [ban_base_arr addObject: arr2];
        [bannerPath_NameArr addObject: [NSString stringWithFormat: @ "%@%@", arr1,   banname]];
        NSLog(@ "%@", bannerPath_NameArr);

Console Error

2016 - 05 - 02 10: 13: 31.173 pageview[949: 22534]( "http://192.168.0.33/cartwebsite3/cdn-images/banner/(\n
\"14613036182de40e0d504f583cda7465979f958a98.jpg\",\n
\"1461303615f99687dd719c4e8bc6a39e946c3d9ef7.jpg\",\n
\"14613036094efdd2f969559e8b1c92e99f32ded48e.jpg\"\n)" )

I got an error like this \n also display unnecessary. I don't know why it is display in URL images.

How can I solve this problem?

2
  • Thank u pang for edited my content Commented May 2, 2016 at 6:29
  • You haven't shared banname assignment, but it looks like it is the whole array of banner_name values (e.g. valueForKey:@"banner_name" rather than looping through the values in data). But until you show us how you created banname, we can't help. Commented May 2, 2016 at 6:34

3 Answers 3

2
NSString *banname = @"14613036182de40e0d504f583cda7465979f958a98.jpg";
NSString *strImageUrl = [NSString stringWithFormat:@"%@",banname];

strImageUrl = [strImageUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];    

[cell.imgView sd_setImageWithURL:[NSURL URLWithString:strImageUrl]
                placeholderImage:[UIImage imageNamed:@"placeholder"]];
Sign up to request clarification or add additional context in comments.

Comments

0

Don't use Nsarray class for concatenate two strings, use 'stringWithFormat', In your json get both string values and merge that with the Nsstring

Check Code below

NSString *banname = @"14613036182de40e0d504f583cda7465979f958a98.jpg";
NSString *imageURL = [NSString stringWithFormat:@"%@%@",[json objectForKey:@"basePath"],banname];

2 Comments

if i used like this banner name only showed in console,it doesn't display banner path
surely it will work, First you get both image path in Nsstring from json, and then concatenate
0
NSString * str1 = [json objectForKey: @ "bannerPath"];
NSString * str2 = [json objectForKey: @ "basePath"];
str1 = [str1 stringByReplacingOccurrencesOfString:@"/" withString:@""];
str2 = [str2 stringByReplacingOccurrencesOfString:@"/" withString:@""];        
NSString *strImage = [NSString stringWithFormat: @ "%@%@", str1,   banname];
imgShow.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:strImage]]];

2 Comments

if used your code, but my console showed like this only bannername 11:24:33.856 pageview[1388:59423] status 200 2016-05-02 11:24:33.856 pageview[1388:59423] ( "14613036182de40e0d504f583cda7465979f958a98.jpg", "1461303615f99687dd719c4e8bc6a39e946c3d9ef7.jpg", "14613036094efdd2f969559e8b1c92e99f32ded48e.jpg" )
Please try to do using break point so we will exactly identify issue

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.