2015-10-24 85 views
-2

因此,我有一個NSDictionary,其中包含各種數據。當打印到日誌中,它打印這樣的:從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的成績或分數的值?

+5

這不是一本字典。這是一系列字典。有一個很大的不同! – matt

+0

如果這是JSON,你沒有使用NSJSONSerialization的任何原因? – matt

+0

噢!這將如何影響我如何訪問數據?我好奇,因爲所有這一切都在一個NSDictionary變量。 – woakley5

回答

2

您的字符串代表NSArray,而不是NSDictionary。這是一個JSON字符串,這樣你就可以使用NSJSONSerialization解析它:

NSString *jsonString = @"..." // your string here 

// Create array from json string 
NSArray *jsonArray = [NSJSONSerialization 
    JSONObjectWithData:[jsonString dataUsingEncoding:NSUTF8StringEncoding] 
       options:NSJSONReadingMutableContainers 
       error:Nil]; 

// Loop to find your user_id 
// Because each child of this array is a dictionary 
for (NSDictionary *dic in jsonArray) { 
    if ([dic[@"user_id"] isEqual:@3016817]) { // user_id field is number 
     // Access what you want 
     NSString *grade = dic[@"grade"]; 

     // For "score" you must go deeper 
     // Just remember, [] is array and {} is dictionary 
    } 
} 
+0

我現在看到!我沒有在for循環中使用dic [@「grade」]部分。謝謝! – woakley5

0

一個簡單的方法來做到這將是

for (NSDictionary* d in theArray) { 
    if ([d[@"user_id"] isEqualToString: u]) { 
     // do something 
    } 
} 

和馬特是正確的,這是字典的一個數組,不管你聲明什麼類型是。

+0

大衛你好!很高興在這裏見到你...... – matt

+0

當我將Array更改爲字典數組並將「u」設置爲我知道存在的user_id時,這會導致應用程序崩潰。 – woakley5

+0

它是因爲'user_id'字段是數字,而不是字符串。你可以用'isEqual:u'來比較它,'u'是'NSNumber'。 – kientux

0

您可以使用下面NSJsonSerialization類的類方法創建的NSArray其中將包含所有的詞典。 (NSData *)數據選項:(NSJSONReadingOptions)選項錯誤:(NSError **)錯誤;

一旦你得到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陣列。