这是我的json回复:
{
"wk_time":{
"user":{
"id":11,
"name":"geetha adhi"
},
"startday":"2015-08-09",
"status":"New",
"total":38.23,
"time_entries":[
{
"id":246,
"project":{
"id":4,
"name":"Vitals"
},
"issue":{
"id":22
},
"user":{
"id":11,
"name":"geetha adhi"
},
"activity":{
"id":8,
"name":"Design"
},
"hours":7.0,
"comments":"test",
"spent_on":"2015-08-09",
"custom_fields":[
{
"id":9,
"name":"Test"
},
{
"id":10,
"name":"test2"
}
]
},
{
"id":287,
"project":{
"id":4,
"name":"Vitals"
},
"issue":{
"id":22
},
"user":{
"id":11,
"name":"geetha adhi"
},
"activity":{
"id":8,
"name":"Design"
},
"hours":5.0,
"comments":"test",
"spent_on":"2015-08-10",
"custom_fields":[
{
"id":9,
"name":"Test"
},
{
"id":10,
"name":"test2"
}
]
},
{
"id":288,
"project":{
"id":4,
"name":"Vitals"
},
"issue":{
"id":22
},
"user":{
"id":11,
"name":"geetha adhi"
},
"activity":{
"id":8,
"name":"Design"
},
"hours":5.0,
"comments":"test",
"spent_on":"2015-08-11",
"custom_fields":[
{
"id":9,
"name":"Test"
},
{
"id":10,
"name":"test2"
}
]
},
{
"id":289,
"project":{
"id":4,
"name":"Vitals"
},
"issue":{
"id":22
},
"user":{
"id":11,
"name":"geetha adhi"
},
"activity":{
"id":8,
"name":"Design"
},
"hours":5.0,
"comments":"test",
"spent_on":"2015-08-12",
"custom_fields":[
{
"id":9,
"name":"Test"
},
{
"id":10,
"name":"test2"
}
]
},
{
"id":291,
"project":{
"id":4,
"name":"Vitals"
},
"issue":{
"id":22
},
"user":{
"id":11,
"name":"geetha adhi"
},
"activity":{
"id":8,
"name":"Design"
},
"hours":5.0,
"comments":"test",
"spent_on":"2015-08-14",
"custom_fields":[
{
"id":9,
"name":"Test"
},
{
"id":10,
"name":"test2"
}
]
},
{
"id":292,
"project":{
"id":4,
"name":"Vitals"
},
"issue":{
"id":22
},
"user":{
"id":11,
"name":"geetha adhi"
},
"activity":{
"id":8,
"name":"Design"
},
"hours":5.0,
"comments":"test",
"spent_on":"2015-08-15",
"custom_fields":[
{
"id":9,
"name":"Test"
},
{
"id":10,
"name":"test2"
}
]
},
{
"id":492,
"project":{
"id":4,
"name":"Vitals"
},
"issue":{
"id":22
},
"user":{
"id":11,
"name":"geetha adhi"
},
"activity":{
"id":9,
"name":"Development"
},
"hours":6.23,
"comments":"ttest",
"spent_on":"2015-08-09",
"custom_fields":[
{
"id":9,
"name":"Test"
},
{
"id":10,
"name":"test2"
}
]
}
]
}
}
我的代码:
NSURLRequest * urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:response url]];
NSError * error = nil;
NSURLResponse * response = nil;
NSData * jsonSource = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:&error];
NSLog(@" %@",jsonSource);
id jsonObjects = [NSJSONSerialization JSONObjectWithData: jsonSource options:NSJSONReadingMutableContainers error:nil];
if ([jsonObjects objectForKey:@"wk_time"] != [NSNull null]) {
NSArray *itemArray = [jsonObjects objectForKey:@"wk_time"];
NSLog(@" %@",itemArray);
}
到目前为止,我做了这么多。我希望得到项目名称,活动名称。怎么弄这个?
答案 0 :(得分:0)
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:responseData options: NSJSONReadingMutableContainers error: &err];
NSDictionary *dictUser = [[jsonDict objectForKey:@"wk_time"]objectForKey:@"user"];
NSArray *array = [[jsonDict objectForKey:@"wk_time"]objectAtIndex:0];
for(int i=0;i<[array count];i++)
{
NSDictionary *dictTimeEntries = [[array objectAtIndex:i]objectForKey:@"time_entries"];
NSDictionary *dictProject = [dictTimeEntries objectForKey:@"project"];
NSString *stringProjectName = [NSString stringWithFormat:@"%@",[dictProject valueForKey@"name"]];
NSLog(@"The stringProjectName is - %@",stringProjectName);
NSString *stingProjectID = [NSString stringWithFormat:@"%@",[dictProject valueForKey@"id"]];
NSLog(@"The stringProjectID is - %@",stingProjectID);
NSDictionary *dictActivity = [dictTimeEntries objectForKey:@"activity"];
NSString *stringActivityName = [NSString stringWithFormat:@"%@",[dictActivity valueForKey@"name"]];
NSLog(@"The stringActivityName is - %@",stringActivityName);
NSString *stingActivityID = [NSString stringWithFormat:@"%@",[dictActivity valueForKey@"id"]];
NSLog(@"The stingActivityID is - %@",stingActivityID);
}
答案 1 :(得分:0)
我强烈建议您将JSON结果转换为object。这是一个非常有用且易于启动的库 - https://github.com/icanzilb/JSONModel
通过这种方式,您可以创建一个代表响应的类,并轻松访问所有属性,例如user.name