2013-02-27 99 views
-2

我在JSON以下響應,我如何解析它解析JSON響應iphone目標C

[{"WordArray":[{"Id":11,"Word1":"ODDER","Category":"Dyr","Differculty":1,"Language":"Danish"},{"Id":225,"Word1":"PRAG","Category":"International by","Differculty":1,"Language":"Danish"}]},{"WordArray":[{"Id":3,"Word1":"TIGER","Category":"Dyr","Differculty":1,"Language":"Danish"},{"Id":154,"Word1":"BOLLE","Category":"Mad og drikke","Differculty":1,"Language":"Danish"}]}, 
{"WordArray":[{"Id":3,"Word1":"TIGER","Category":"Dyr","Differculty":1,"Language":"Danish"},{"Id":147,"Word1":"PIZZA","Category":"Mad og drikke","Differculty":1,"Language":"Danish"}]}] 

我曾嘗試沒有運氣以下不確定

NSMutableArray* json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error]; 

NSMutableArray * roundWords = [[NSMutableArray alloc]retain ]; 

roundWords = [json objectAtIndex:i]; 

JSON包含了我3'WordArray',但我不確定如何繼續.. 如何迭代在WordArray和NSLog Word1值中的每個項目,例如?

謝謝!

+0

另外請注意,這甚至與Xcode沒有遠程相關。 – 2013-02-27 18:25:31

回答

1

我如何遍歷在WordArray和NSLog的每個項目的字1的值

使用快速枚舉。

for (NSDictionary *dict in array) { 
    NSLog(@"%@", [dict objectForKey:@"Word1"]); 
} 

順便問一下,你的代碼是錯誤的,在幾個地方:

NSMutableArray *roundWords = [[NSMutableArray alloc] retain]; 
roundWords = [json objectAtIndex:i]; 

其中,一不使用retain鏈與alloc這樣。使用init來初始化一個對象。

二:除非你使用ARC(你沒有),你與該行的內存泄漏 - 指針到原來的數組,因爲重新分配別的事情被丟棄。

三:[json objectAtIndex:i]不是NSMutableArray - 這是一個NSDictionary(這是不可改變的的方式聲明不是魔術 - 但是你宣佈你的變量,它不會影響NSJSONSerialization返回一個不可改變的事實。字典,除非另有規定)。