gpt4 book ai didi

ios - JSON - 将 JSON 数据提取到数据模型中

转载 作者:行者123 更新时间:2023-11-29 01:37:54 26 4
gpt4 key购买 nike

我尝试将 JSON 数据存储到数据模型中以使其易于使用,但它只存储一个数据,即 NSString *status"它不存储另外两个(“NSArray * content”和“NSString * serverTime”)。

这是我第二次使用JSON,所以我对它了解不多。

这是来自 NSLog(@"item %@",itemDic);

2015-09-24 23:11:40.697 fott[628:98405] item status

这是错误日志。

2015-09-24 23:11:40.698 fott[628:98405] -[NSTaggedPointerString countByEnumeratingWithState:objects:count:]: unrecognized selector sent to instance 0xa007375746174736

这是我的代码

- (NSArray*)getData{
NSMutableArray *result = [[NSMutableArray alloc] init];

NSString *path =@"http://87.251.89.41/application/11424/article/get_articles_list";
if(path){
NSURL *json = [NSURL URLWithString:path]; // jsonString

NSData *jsonData = [NSData dataWithContentsOfURL:json];
encoding:NSUTF8StringEncoding];

if(jsonData){
NSError *error = nil;
NSArray *jsonObjects = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
NSLog(@"jsonObjects %@",jsonObjects);

for(NSDictionary *itemDic in jsonObjects){
PlaceData *item = [[PlaceData alloc] init];
NSLog(@"item %@",itemDic);
for(NSString *key in itemDic){

if([item respondsToSelector:NSSelectorFromString(key)]){

if([key isEqualToString:@"content"]){

NSArray *contentJsonObjects = [itemDic objectForKey:key];
NSMutableArray *contents = [[NSMutableArray alloc] init];

for(NSDictionary *contentDic in contentJsonObjects){
Content *content = [[Content alloc] init];

for(NSString *contentKey in contentDic){
if([contentKey isEqualToString:@"content"]){

NSArray * contentDetailJsonObjects = [contentDic objectForKey:contentKey];
NSMutableArray * contentDetails = [[NSMutableArray alloc] init];

for(NSDictionary *contentDetailDic in contentDetailJsonObjects){
Contents * contentDetail = [[Contents alloc]init];

for (NSString * contentDetailKey in contentDetailDic){
[contentDetail setValue:[contentDetailDic valueForKey:contentDetailKey] forKey:contentDetailKey];
}
[contentDetails addObject:contentDetail];
}
[content setValue:contentDetails forKey:contentKey];
}


[content setValue:[contentDic valueForKey:contentKey] forKey:contentKey];
}



[contents addObject:content];
}
// Set value to content
[item setValue: contents forKey:key];
}else{
// For general detail
[item setValue:[itemDic valueForKey:key] forKey:key];

}
}
}

[result addObject:item];
}
}else{
NSLog(@"JSON NOT FOUND");
}
}else{
NSLog(@"PATH NOT FOUND");
}

return result;
}

@end

这是一些 JSON 数据

 {
"status": "success",
"content": [
{
"id": 11447,
"title": "Arsenal's Afobe targets a full season on loan",
"dateTime": "30.10.2014 06:38",
"tags": [],
"content": [
{
"type": "text",
"subject": "Benik Afobe",
"description": "The 21-year-old was last capped by the Three Lions in February 2013 when he featured for the Under-21s against Sweden at Walsall in a 4-0 win.\r\n\r\nA series of injuries since then have restricted his progress both on the club and international front and he spent the second half of last season on loan at Sheffield Wednesday."
}
],
"ingress": "Arsenal and England Under-21s striker Afobe is hoping a full season on loan can help to rejuvenate his career.\r\n",
"image": "http://87.251.89.41/sites/default/files/afobe-celeb-dier.jpg",
"created": 1407476424,
"changed": 1414664497
},
{
"id": 11445,
"title": "Alex Pritchard",
"dateTime": "03.12.2014 06:36",
"tags": [
"11457"
],
"content": [
{
"type": "text",
"subject": "Alex Pritchard",
"description": "After impressing at the County Ground, he has agreed a similar spell at Griffin Park as well as extending his contract with Spurs until 2016.\r\n\r\n\"Alex is a player I have watched for many years,\" revealed Brentford boss Mark Warburton to BrentfordFC.co.uk.\r\n\r\n\"I have followed his career at Tottenham from a young age. He is very good tactically and technically with excellent dead-ball delivery."
}
],
"ingress": "Spurs starlet Alex Pritchard signs on loan for Brentford",
"image": "http://87.251.89.41/sites/default/files/alex-pritchard.jpg",
"created": 1407476299,
"changed": 1417695236
}
],
"serverTime": 1443015978
}

最佳答案

听起来您会从试用 JSONModel 中获益

http://www.jsonmodel.com/

导入框架,修改您的模型以完全匹配 JSON,JSONModel 将为您完成剩下的工作

关于ios - JSON - 将 JSON 数据提取到数据模型中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32766541/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com