Objective c 使用webservice xcode 5获取事件的标题和图像

Objective c 使用webservice xcode 5获取事件的标题和图像,objective-c,string,parsing,Objective C,String,Parsing,正如标题所说,我尝试使用webservice获取标题名称和图像,以便通过MySQL数据库动态更改它。 以下是webservice结果: {"event":[{"event_id":"1","title":"grand meeting","logo":"grandmeeting.png"}],"succeed":1} 这是我的视图控制器 @property (weak, nonatomic) IBOutlet UILabel *titleText; @property (weak, nonato

正如标题所说,我尝试使用webservice获取标题名称和图像,以便通过MySQL数据库动态更改它。 以下是webservice结果:

{"event":[{"event_id":"1","title":"grand meeting","logo":"grandmeeting.png"}],"succeed":1}
这是我的视图控制器

@property (weak, nonatomic) IBOutlet UILabel *titleText;
@property (weak, nonatomic) IBOutlet UIImageView *imageText;
这是我获取Web服务的代码

NSString *strURL=[NSString stringWithFormat:@"http://localhost/service.php"];
NSData *dataURL=[NSData dataWithContentsOfURL: [NSURL URLWithString:strURL]];
NSString *strResult=[[NSString alloc] initWithData:dataURL encoding:NSUTF8StringEncoding];
现在我的问题是我不知道如何将strResult解析为title和image。
有人能帮我吗?

您可以尝试下面的代码来解析json数据

NSMutableDictionary *dict = [NSJSONSerialization JSONObjectWithData:dataFromUrl options: NSJSONReadingMutableContainers error:&err];

NSMutableArray *array = dict[@"event"];
NSMutableDictionary *eventDict = [array objectAtIndex:0];


NSString *title = eventDict[@"title"];
NSString *logo = eventDict[@"logo"];

您可以尝试以下代码来解析json数据

NSMutableDictionary *dict = [NSJSONSerialization JSONObjectWithData:dataFromUrl options: NSJSONReadingMutableContainers error:&err];

NSMutableArray *array = dict[@"event"];
NSMutableDictionary *eventDict = [array objectAtIndex:0];


NSString *title = eventDict[@"title"];
NSString *logo = eventDict[@"logo"];
如果您想在图像视图中显示这个来自url的图像,只需放置以下代码

[ImageView setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSString stringWithFormat:@"http:// your web address /%@",imageNameStr]]]];
如果您想在图像视图中显示这个来自url的图像,只需放置以下代码

[ImageView setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSString stringWithFormat:@"http:// your web address /%@",imageNameStr]]]];

给你…我试过了,但它发送了一个未捕获的异常,上面写着“NSInvalidArgumentException”,原因:“-[NSCFDictionary OBJECTATINEDEXCEPTION::未识别的bla bla bla…”给你…我试过了,但它发送了一个未捕获的异常,上面写着“NSInvalidArgumentException”,原因:“-[NSCFDictionary OBJECTINATINEDEXCEPTION:]:unrecognized bla bla bla..'我尝试了你的代码,但它抛出了以下错误:由于未捕获的异常“NSInvalidArgumentException”终止应用程序,原因:'-[\uu NSArrayI length]:未识别的选择器发送到实例0x9d4e570'只是一个缩影,我没有看到我正在重新编辑代码的数组。与往常一样,Bhaskar工作得很好。非常感谢您的努力^^但是,我尝试使用您的代码显示图像,它说了这个错误:-[\uu NSCFString isFileURL]:发送到实例0x9E57270的无法识别的选择器抱歉Bhaskar再次打扰,我尝试使用您的代码显示图像,它说这个错误:-[\uu NSCFString isFileURL]:发送到实例0x9e57270的选择器无法识别。我用我的插座(即_loginImage)及其UIImageview*loginImage更改ImageView。谢谢你可以发布设置图像的代码或发布此图像的基本url示例:我尝试了你的代码,但它引发了此错误:由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:'-[\u NSArrayI length]:无法识别的选择器发送到实例0x9d4e570'只是一个小例子,我没有看到我正在重新编辑代码的数组。与往常一样,Bhaskar工作得非常好。非常感谢您的努力^^但是,我尝试使用您的代码显示图像,它说了这个错误:-[\uu NSCFString isFileURL]:发送到实例0x9E57270的无法识别的选择器抱歉Bhaskar再次打扰,我尝试使用您的代码显示图像,它说这个错误:-[\uu NSCFString isFileURL]:发送到实例0x9e57270的选择器无法识别。我用我的插座(即_loginImage)及其UIImageview*loginImage更改ImageView。谢谢您可以发布设置图像的代码或发布此图像的基本url示例: