如何在iOS中显示来自JSON URL的图像?

时间:2016-05-02 05:16:37

标签: ios objective-c json parsing

我在显示JSON网址图片时遇到问题。我想连接两个字符串来显示JSON URL中的图像,但我在控制台中出错,我无法显示图像。

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"
        }
    }
}

代码:

 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);

控制台错误

  

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)的"         )

我收到了这样的错误\ n也显示不必要的错误。我不知道为什么它会显示在URL图片中。

我该如何解决这个问题?

3 个答案:

答案 0 :(得分:2)

static willTransitionTo(transition){
  var state = ChatStore.getState();
  if(!state.user){
    transition.redirect('/login');
  }
}

答案 1 :(得分:0)

不要使用Nsarray类来连接两个字符串,使用' stringWithFormat',在你的json中获取两个字符串值并将其与Nsstring合并

检查以下代码

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

答案 2 :(得分: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]]];