如何解析 json 并将其填充到模型中?
how to parse json and fill it in model?
这是我的json
[
{
"_id": "58b517e8dd7fe4a90fcadc44",
"isActive": false,
"balance": ",293.72",
"picture": "http://placehold.it/32x32",
"age": 38,
"eyeColor": "brown",
"name": "Finch Hayes",
"gender": "male",
"company": "NIKUDA",
"email": "finchhayes@nikuda.com",
"phone": "+1 (874) 422-3921",
"address": "227 Trucklemans Lane, Steinhatchee, New Hampshire, 9835",
"about": "Veniam pariatur exercitation consequat exercitation dolore sint labore consequat enim cupidatat pariatur elit. Anim officia velit aliqua anim consectetur mollit aliquip occaecat duis non. Ea voluptate velit eu elit qui nulla aliquip.\r\n",
"friends": [
{
"id": 0,
"name": "Mooney Bond"
},
{
"id": 1,
"name": "Rosie Owen"
},
{
"id": 2,
"name": "Melanie Brown"
}
]
},
{
"_id": "58b517e8b53b162133de0013",
"isActive": true,
"balance": ",637.14",
"picture": "http://placehold.it/32x32",
"age": 29,
"eyeColor": "green",
"name": "Terry Conway",
"gender": "male",
"company": "MEDALERT",
"email": "terryconway@medalert.com",
"phone": "+1 (856) 436-2212",
"address": "904 Carlton Avenue, Carrizo, Nevada, 9560",
"about": "Aute duis esse enim sit occaecat veniam aute sunt esse. Quis consequat dolore veniam reprehenderit laborum. Labore quis magna cillum consequat laborum amet in amet proident sit.\r\n",
"friends": [
{
"id": 0,
"name": "Sparks Baxter"
},
{
"id": 1,
"name": "Carrillo Gonzales"
},
{
"id": 2,
"name": "Hebert Montgomery"
}
]
}]
我已经创建了 person 和 personFriend 对象。下面是我的 main.m
NSError *error;
NSString *url_string = [NSString stringWithFormat:@"http://danialm.weebly.com/uploads/1/0/1/5/101578472/people.json"];
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:url_string]];
id personJson = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
//NSLog(@"item: %@", personJson);
NSMutableArray *personArray = [[NSMutableArray alloc]init];
NSMutableArray *friendArray = [[NSMutableArray alloc]init];
for (NSDictionary *personInfoDict in personJson) {
Person *person = [[Person alloc] init];
person.personId = [personInfoDict objectForKey:@"_id"];
person.about = [personInfoDict objectForKey:@"about"];
person.address = [personInfoDict objectForKey:@"address"];
person.age = [[personInfoDict objectForKey:@"age"] doubleValue];
person.balance = [personInfoDict objectForKey:@"balance"];
person.company = [personInfoDict objectForKey:@"company"];
person.email = [personInfoDict objectForKey:@"email"];
person.eyeColor = [personInfoDict objectForKey:@"eyeColor"];
person.gender = [personInfoDict objectForKey:@"gender"];
person.isActive = [[personInfoDict objectForKey:@"isActive"]boolValue];
person.name = [personInfoDict objectForKey:@"name"];
person.phone = [personInfoDict objectForKey:@"phone"];
person.picture = [personInfoDict objectForKey:@"picture"];
person.friends = [personInfoDict objectForKey:@"friends"];
for (NSDictionary *friendInfoDict in personInfoDict) {
PersonFriends *friend = [[PersonFriends alloc]init];
friend.friendsId = [[friendInfoDict objectForKey:@"id"]doubleValue];
friend.name = [friendInfoDict objectForKey:@"name"];
[friendArray addObject: friend];
}
[personArray addObject:person];
[personArray addObject:friendArray];
}
NSLog(@"personArray: %@", personArray);
我不知道如何解析 json 并将其填入 model.this 是正确的方法还是错误的,因为我在 objective c 开发中仍然是新手。
大部分看起来还不错。几个问题:
- 您应该将
NSMutableArray *friendArray = [[NSMutableArray alloc]init];
移动到您的第一个 for 循环内,就在您创建好友对象的第二个 for 循环之前。这是因为每个人都有自己的好友列表,所以需要新建一个数组。
for (NSDictionary *friendInfoDict in personInfoDict)
应该是 for (NSDictionary *friendInfoDict in [personInfoDict objectForKey:@"friends"])
。否则你会尝试为每个人创建一个朋友。
person.friends = [personInfoDict objectForKey:@"friends"];
应该是person.friends = friendArray;
,并且应该移到你的好友循环之后。
- 您实际上并不需要
[personArray addObject:friendArray];
,因为每个人对象应该已经有一组朋友。
此外,问题不大,代码可读性更高 - 但值得在 Person 和 PersonFriend 对象中移动大量代码。这样你的循环就变成了:
for (NSDictionary *personInfoDict in personJson) {
Person *person = [[Person alloc] initWithDictionary:personInfoDict];
[personArray addObject:person];
}
在你的 Person initWithDictionary
中,你将拥有所有代码:
- (instancetype)initWithDictionary:(NSDictionary *)personInfoDict {
if (self = [super init]) {
self.personId = [personInfoDict objectForKey:@"_id"];
NSMutableArray *friendArray = [[NSMutableArray alloc]init];
for (NSDictionary *friendInfoDict in [personInfoDict objectForKey:@"friends"]) {
PersonFriends *friend = [[PersonFriends alloc]initWithDictionary:friendInfoDict];
[friendArray addObject: friend];
}
...etc
}
return self;
}
编辑:
要查找特定的人,您可以编写一个辅助方法来遍历您的人员数组和 return 具有给定 ID 的人(例如,可以使用姓名等):
- (Person *)personWithId:(NSString *)id {
for (Person *person in self.personArray) {
if ([person.id isEqualToString:id]) {
return person;`
}
}
return nil;
}
尽管您需要将您的 personArray 传入,或者将其设为 属性 以便您的辅助方法可以访问它(可能最好将其设为 属性)。
您应该使用非常易于使用的 JSONAccelerator iOS 应用程序。
通过它您可以在几秒钟内解析和创建模型。
这是我的json
[
{
"_id": "58b517e8dd7fe4a90fcadc44",
"isActive": false,
"balance": ",293.72",
"picture": "http://placehold.it/32x32",
"age": 38,
"eyeColor": "brown",
"name": "Finch Hayes",
"gender": "male",
"company": "NIKUDA",
"email": "finchhayes@nikuda.com",
"phone": "+1 (874) 422-3921",
"address": "227 Trucklemans Lane, Steinhatchee, New Hampshire, 9835",
"about": "Veniam pariatur exercitation consequat exercitation dolore sint labore consequat enim cupidatat pariatur elit. Anim officia velit aliqua anim consectetur mollit aliquip occaecat duis non. Ea voluptate velit eu elit qui nulla aliquip.\r\n",
"friends": [
{
"id": 0,
"name": "Mooney Bond"
},
{
"id": 1,
"name": "Rosie Owen"
},
{
"id": 2,
"name": "Melanie Brown"
}
]
},
{
"_id": "58b517e8b53b162133de0013",
"isActive": true,
"balance": ",637.14",
"picture": "http://placehold.it/32x32",
"age": 29,
"eyeColor": "green",
"name": "Terry Conway",
"gender": "male",
"company": "MEDALERT",
"email": "terryconway@medalert.com",
"phone": "+1 (856) 436-2212",
"address": "904 Carlton Avenue, Carrizo, Nevada, 9560",
"about": "Aute duis esse enim sit occaecat veniam aute sunt esse. Quis consequat dolore veniam reprehenderit laborum. Labore quis magna cillum consequat laborum amet in amet proident sit.\r\n",
"friends": [
{
"id": 0,
"name": "Sparks Baxter"
},
{
"id": 1,
"name": "Carrillo Gonzales"
},
{
"id": 2,
"name": "Hebert Montgomery"
}
]
}]
我已经创建了 person 和 personFriend 对象。下面是我的 main.m
NSError *error;
NSString *url_string = [NSString stringWithFormat:@"http://danialm.weebly.com/uploads/1/0/1/5/101578472/people.json"];
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:url_string]];
id personJson = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
//NSLog(@"item: %@", personJson);
NSMutableArray *personArray = [[NSMutableArray alloc]init];
NSMutableArray *friendArray = [[NSMutableArray alloc]init];
for (NSDictionary *personInfoDict in personJson) {
Person *person = [[Person alloc] init];
person.personId = [personInfoDict objectForKey:@"_id"];
person.about = [personInfoDict objectForKey:@"about"];
person.address = [personInfoDict objectForKey:@"address"];
person.age = [[personInfoDict objectForKey:@"age"] doubleValue];
person.balance = [personInfoDict objectForKey:@"balance"];
person.company = [personInfoDict objectForKey:@"company"];
person.email = [personInfoDict objectForKey:@"email"];
person.eyeColor = [personInfoDict objectForKey:@"eyeColor"];
person.gender = [personInfoDict objectForKey:@"gender"];
person.isActive = [[personInfoDict objectForKey:@"isActive"]boolValue];
person.name = [personInfoDict objectForKey:@"name"];
person.phone = [personInfoDict objectForKey:@"phone"];
person.picture = [personInfoDict objectForKey:@"picture"];
person.friends = [personInfoDict objectForKey:@"friends"];
for (NSDictionary *friendInfoDict in personInfoDict) {
PersonFriends *friend = [[PersonFriends alloc]init];
friend.friendsId = [[friendInfoDict objectForKey:@"id"]doubleValue];
friend.name = [friendInfoDict objectForKey:@"name"];
[friendArray addObject: friend];
}
[personArray addObject:person];
[personArray addObject:friendArray];
}
NSLog(@"personArray: %@", personArray);
我不知道如何解析 json 并将其填入 model.this 是正确的方法还是错误的,因为我在 objective c 开发中仍然是新手。
大部分看起来还不错。几个问题:
- 您应该将
NSMutableArray *friendArray = [[NSMutableArray alloc]init];
移动到您的第一个 for 循环内,就在您创建好友对象的第二个 for 循环之前。这是因为每个人都有自己的好友列表,所以需要新建一个数组。 for (NSDictionary *friendInfoDict in personInfoDict)
应该是for (NSDictionary *friendInfoDict in [personInfoDict objectForKey:@"friends"])
。否则你会尝试为每个人创建一个朋友。person.friends = [personInfoDict objectForKey:@"friends"];
应该是person.friends = friendArray;
,并且应该移到你的好友循环之后。- 您实际上并不需要
[personArray addObject:friendArray];
,因为每个人对象应该已经有一组朋友。
此外,问题不大,代码可读性更高 - 但值得在 Person 和 PersonFriend 对象中移动大量代码。这样你的循环就变成了:
for (NSDictionary *personInfoDict in personJson) {
Person *person = [[Person alloc] initWithDictionary:personInfoDict];
[personArray addObject:person];
}
在你的 Person initWithDictionary
中,你将拥有所有代码:
- (instancetype)initWithDictionary:(NSDictionary *)personInfoDict {
if (self = [super init]) {
self.personId = [personInfoDict objectForKey:@"_id"];
NSMutableArray *friendArray = [[NSMutableArray alloc]init];
for (NSDictionary *friendInfoDict in [personInfoDict objectForKey:@"friends"]) {
PersonFriends *friend = [[PersonFriends alloc]initWithDictionary:friendInfoDict];
[friendArray addObject: friend];
}
...etc
}
return self;
}
编辑: 要查找特定的人,您可以编写一个辅助方法来遍历您的人员数组和 return 具有给定 ID 的人(例如,可以使用姓名等):
- (Person *)personWithId:(NSString *)id {
for (Person *person in self.personArray) {
if ([person.id isEqualToString:id]) {
return person;`
}
}
return nil;
}
尽管您需要将您的 personArray 传入,或者将其设为 属性 以便您的辅助方法可以访问它(可能最好将其设为 属性)。
您应该使用非常易于使用的 JSONAccelerator iOS 应用程序。 通过它您可以在几秒钟内解析和创建模型。