您好我是ios编程和学习的东西我正在通过Web服务调用和以json格式获取响应。我成功了但我不知道如何获取(解析)响应并循环响应。我回复如下,我的字典如下: 的 JSON
{
"contacts": [
{
"id": "c200",
"name": "Ravi Tamada",
"email": "ravi@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "male",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
},
{
"id": "c201",
"name": "Johnny Depp",
"email": "johnny_depp@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "male",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
},
{
"id": "c202",
"name": "Leonardo Dicaprio",
"email": "leonardo_dicaprio@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "male",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
},
{
"id": "c203",
"name": "John Wayne",
"email": "john_wayne@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "male",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
},
{
"id": "c204",
"name": "Angelina Jolie",
"email": "angelina_jolie@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "female",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
},
{
"id": "c205",
"name": "Dido",
"email": "dido@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "female",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
},
{
"id": "c206",
"name": "Adele",
"email": "adele@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "female",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
},
{
"id": "c207",
"name": "Hugh Jackman",
"email": "hugh_jackman@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "male",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
},
{
"id": "c208",
"name": "Will Smith",
"email": "will_smith@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "male",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
},
{
"id": "c209",
"name": "Clint Eastwood",
"email": "clint_eastwood@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "male",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
},
{
"id": "c2010",
"name": "Barack Obama",
"email": "barack_obama@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "male",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
},
{
"id": "c2011",
"name": "Kate Winslet",
"email": "kate_winslet@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "female",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
},
{
"id": "c2012",
"name": "Eminem",
"email": "eminem@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "male",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
}
]
}
**code**
NSString *mydata = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"====My data from webservice is===%@",mydata);
NSError *e = nil;
//saving data in dictionary.
NSDictionary *recipeDictionary = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&e];
NSLog(@"My Dictionary is %@", recipeDictionary);
答案 0 :(得分:2)
很简单。
根据您的回复,确认您的回复是字典。在您的recipeDictionary中,有一个与联系人密钥关联的数组对象。
所以在数组中取这个值。就像这个
NSArray *yourContactsArray = [recipeDictionary valueForKey:@"contacts"];
现在这个数组包含了多个以字典形式存在的对象。我正在打印名称键中的所有值以演示你的提取。
for (NSDictionary *dict in yourContactsArray)
{
NSString *name = [dict valueForKey:@"name"];
NSLog(@"%@",name);
}
答案 1 :(得分:1)
我建议您创建一个用于存储来自Web服务的数据的模型类。将该类命名为Contact
<强> Contact.h 强>
@interface Contact : NSObject
@property(nonatomic, strong) NSString *contactId;
@property(nonatomic, strong) NSString *name;
@property(nonatomic, strong) NSString *email;
@property(nonatomic, strong) NSString *address;
@property(nonatomic, strong) NSString *gender;
@property(nonatomic, strong) NSString *mobilePhone;
@property(nonatomic, strong) NSString *homePhone;
@property(nonatomic, strong) NSString *officePhone;
@end
在您的Contact.m文件中
@implementation Contacts
- (id)initWithDictionary:(NSMutableDictionary *)dict{
self = [super init];
if(self) {
//Parse your dictionary into objects here
_contactId = [dict objectForKey:@"id"];
_email = [dict objectForKey:@"email"];
_name = [dict objectForKey:@"name"];
_address = [dict objectForKey:@"address"];
_gender = [dict objectForKey:@"gender"];
_homePhone = [[dict objectForKey:@"phone"] objectForKey:@"home"];
_mobilePhone = [[dict objectForKey:@"id"] objectForKey:@"mobile"];
_officePhone = [[dict objectForKey:@"id"] objectForKey:@"office"];
}
return self;
}
@end
在你的班级
NSString *mydata = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"====My data from webservice is===%@",mydata);
NSError *e = nil;
//saving data in dictionary.
NSDictionary *recipeDictionary = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&e];
NSLog(@"My Dictionary is %@", recipeDictionary);
NSMutableArray *contacts = [[NSMutableArray alloc] init];
for (NSDictionary *contactDict in [recipeDictionary objectForKey:@"contacts"]) {
Contact *contact = [[Contact alloc] initWithDictionary:contactDict];
NSLog(@"Name %@",contact.name); //Prints Name
[contacts addObject: contact];
}
contacts数组将包含联系对象数组。您可以使用点表示法来提取特定信息 您可以将contacts数组传递给应用程序的任何部分。对象优于字典
答案 2 :(得分:0)
您应该做的主要事情是将JSON响应转换为NSString / String 我建议使用这些库来解析JSON。
Objective-C版本:
https://github.com/TouchCode/TouchJSON
Swift版本: