所以我有一个NSDictionary,其中包含各种数据。打印到日志时,它打印如下:
[{"user_id":3016817,"grade":"A","percent":"93","grading_periods":[{"assignments":[{"points":100.0,"grade":"A","score":95.0,"percent":"93","comment":null,"id":3268180},{"points":100.0,"grade":"A","score":90.0,"percent":"93","comment":null,"id":3268181}],"grade":"A","percent":"93","name":"Default"}]},{"user_id":3016818,"grade":"A","percent":"94","grading_periods":[{"assignments":[{"points":100.0,"grade":"A","score":92.0,"percent":"94","comment":null,"id":3268180},{"points":100.0,"grade":"A","score":95.0,"percent":"94","comment":null,"id":3268181}],"grade":"A","percent":"94","name":"Default"}]}]
如果我在线使用格式化程序,它的可读性更高,看起来像这样:
[
{
"user_id": 3016817,
"grade": "A",
"percent": "93",
"grading_periods": [
{
"assignments": [
{
"points": 100,
"grade": "A",
"score": 95,
"percent": "93",
"comment": null,
"id": 3268180
},
{
"points": 100,
"grade": "A",
"score": 90,
"percent": "93",
"comment": null,
"id": 3268181
}
],
"grade": "A",
"percent": "93",
"name": "Default"
}
]
},
{
"user_id": 3016818,
"grade": "A",
"percent": "94",
"grading_periods": [
{
"assignments": [
{
"points": 100,
"grade": "A",
"score": 92,
"percent": "94",
"comment": null,
"id": 3268180
},
{
"points": 100,
"grade": "A",
"score": 95,
"percent": "94",
"comment": null,
"id": 3268181
}
],
"grade": "A",
"percent": "94",
"name": "Default"
}
]
}
]
我的问题是如何使用此词典访问特定user_id的成绩或分数值?
答案 0 :(得分:2)
您的字符串代表name,tags,time,mean
server_1,a,2015-10-24T11:45:27.869918Z,620023.5521262008
server_2,b,2015-10-24T11:45:27.869918Z,0
server_3,c,2015-10-24T11:45:27.869918Z,0
server_4,d,2015-10-24T11:45:27.869918Z,66.16323731138544
,而不是NSArray
。它是一个JSON字符串,因此您可以使用NSDictionary
解析它:
NSJSONSerialization
答案 1 :(得分:0)
执行此操作的简单方法是
for (NSDictionary* d in theArray) {
if ([d[@"user_id"] isEqualToString: u]) {
// do something
}
}
马特是对的,它是一系列词典,无论你声称是什么类型。
答案 2 :(得分:0)
您可以使用NSJsonSerialization类的以下类方法来创建包含所有字典的NSArray。
+(id)JSONObjectWithData:(NSData *)数据选项:(NSJSONReadingOptions)opt error:(NSError **)error;
从JSON获得字典数组后,您可以执行以下操作:
NSArray *filteredjsonArr = [jsonArr filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"user_id == %@", @"3016818"]];
NSDictionary *dict = [filtered firstObject];
if (dict != nil) {
NSString *grade = [dict objectForKey:@"grade"];
NSArray *gradingPeriods = [dict objectForKey:@"grading_periods"];
}
要访问特定作业的分数和成绩,您需要深入了解gradingPeriods数组。