2014-02-27 64 views
0

我有一個JSON數組得到數組,像這樣:如何從花葯陣列

a= {"title":"workers","data":[{"name":"tom","id":"LBJP01Z"},{"name":"bob","id":"LBJP08Z"},{"name":"bill","id":"LBJP02Z"}]},{"title":"teachers","data":[{"name":"jill","id":"LZJP01Z"},{"name":"tim","id":"LBJP03Z"},{"name":"sam","id":"LBJP07Z"}]} 

我想要得到的結果是這樣的:

tom 
bob 
bill 
jill 
tim 
sam 

我的代碼:

for (int i = 0; i < [a count]-1; i++) { 

    for (int j = 0; j < [a[i] count]-1; j++) 
    { 

     NSString *str = [NSString stringWithFormat:@"%@",[[[[a objectAtIndex:i]objectForKey:@"data"]objectAtIndex:j] objectForKey:@"name"]]; 
     NSLog(@"%@",str); 

    } 
} 

但最終,我得到如下結果:

tom 
tom 
tom 
tom 
tom 
tom 
+1

轉到json.org所和學習JSON語法。學習只需要5-10分鐘。然後你會更好地理解你在做什麼。 –

回答

1

從您的JSON中,a是一個字典,而不是一個數組。獲取data陣列啓動:

NSArray *dataArray = a[@"data"]; 

現在,使用KVC提取名稱:

NSArray *names = [dataArray valueForKey:@"name"]; 
0
NSMutableDictionary *yourdict = [a JSONValue]; 
NSMutableArray *my_arr = [get_news objectForKey:@"data"]; 
[my_arr retain];