这里我得到的json数组包含多个字典。
(
{
text = Aaa;
title = 1;
},
{
text = Bbb;
title = 2;
}
)
我试过这个
for(NSDictionary * dic in array)
{
NSString * strTitle =[dic valueForKey:@"Title"];
NSString * strDescription =[dic valueForKey:@"Description"];
NSString * strSatartDate =[dic valueForKey:@"StartTime"];
NSString * strEndDate =[dic valueForKey:@"EndTime"];
NSString * strDay =[dic valueForKey:@"MeetingDate"];
NSString * strEPid =[dic valueForKey:@"EPId"];
objModel.Titlestr=strTitle;
objModel.Descriptionstr=strDescription;
objModel.StartTimeStr=strSatartDate;
objModel.EndTimeStr=strEndDate;
objModel.Daystr=strDay;
objModel.EPId=strEPid;
}
所以这里我想生成带有2个字典的obj,但这对jme不起作用。
我想把它放到NSObject中我怎么能这样做。
答案 0 :(得分:0)
所有对象都从Objective C中的NSObject继承。
以下是解析数据的方法。
NSArray *jsonArray = [NSJSONSerialization:data //data is NSData type
options:NSJSONReadingMutableContainers
error:nil];
for (NSDictionary * data in jsonArray) {
NSString * text = data[@"text"];
int title = [data[@"title"]intValue];
NSLog(@"text=%@ and title=%d", text, title);
}
输出:
text=Aaa and title=1
text=Baa and title=2
如果你的模型属性应该包含多个值,则不能使用NSString。
您将需要一个数组,一个可变数组,以便您可以向其追加值
for(NSDictionary * dic in array){
NSString * strTitle =[dic valueForKey:@"Title"];
NSString * strDescription =[dic valueForKey:@"Description"];
NSString * strSatartDate =[dic valueForKey:@"StartTime"];
NSString * strEndDate =[dic valueForKey:@"EndTime"];
NSString * strDay =[dic valueForKey:@"MeetingDate"];
NSString * strEPid =[dic valueForKey:@"EPId"];
[objModel.Titlestr addObject:strTitle];
[objModel.Descriptionstr addObject:strDescription];
[objModel.StartTimeStr addObject:strSatartDate];
[objModel.EndTimeStr addObject:strEndDate];
[objModel.Daystr addObject:strDay];
[objModel.EPId addObject:strEPid];
}