2011-08-12 81 views
0

我正在iphone應用程序上工作。在這裏,我讀的JSON數組我得到它的值到數組我得到以下格式的值:閱讀JSON數組

Array : { 
global =  { 
    players =   { 
     1 = "John Doe, A school"; 
     10 = "Jonathan Doe, Another school"; 
     2 = "Joe Doe, Another school"; 
     3 = "Jane Doe, A school"; 
     4 = "Jay Doe, Another school"; 
     5 = "Jimmy Doe, A school"; 
     6 = "Jeremy Doe, Another school"; 
     7 = "Johnny Doe, A school"; 
     8 = "Jeremiah Doe, Another school"; 
     9 = "Jennifer Doe, A school"; 
    }; 
    schools =   { 
     1 = "A school"; 
     2 = "Another school"; 
     3 = "The school"; 
     4 = "The other school"; 
     5 = "That school"; 
    }; 
    text = "A dynamic text that needs to be displayed to the player."; 
}; 

現在我的問題是,我怎麼能讀的全球各個部分?讀完之後,我如何閱讀學校? etc ...

+0

我假設你在Objective-C編碼 - 是這種情況?或者你在使用Javascript(例如Titanium,PhoneGap等) –

回答

0

使用TouchJSON。它使用起來非常簡單,並且具有全面的文檔。

0

只是這樣做

for (var i in global) { 
    for(var j in global [i].schools){ 
    alert(global [i].schools[j]); 
    }     
} 
0

SBJson是另一個很好的Objective-C JSON庫。它會將JSON解析爲嵌套的NSArray和NSDictionary對象。典型用法是這樣的:

#import "SBJson.h" 

NSString *jsonString = @"{ 
    'name': 'Simon', 
    'address': {'street': '1 High Street', 'town': 'Anytown'} 
}"; 
NSDictionary *jsonData = [jsonString JSONValue]; 

NSLog(@"Simon lives in %@", [jsonData valueForKeyPath:@"address.town"]); 

輸出:

Simon lives in Anytown 

順便說一句,而不是使用詞典(由大括號和鍵表示:值數據表),如果你想要一個有序的數據列表(例如在你的例子中爲players),你可以使用一個列表(方括號括住一個值列表)。所以你的數據變成:

data: { 
    global: { 
     players: [ 
      "John Doe, A school"; 
      "Joe Doe, Another school"; 
      "Jane Doe, A school"; 
      "Jay Doe, Another school"; 
      "Jimmy Doe, A school"; 
      "Jeremy Doe, Another school"; 
      "Johnny Doe, A school"; 
      "Jeremiah Doe, Another school"; 
      "Jennifer Doe, A school"; 
      "Jonathan Doe, Another school"; 
     ] 
     schools: [ 
      "A school"; 
      "Another school"; 
      "The school"; 
      "The other school"; 
      "That school"; 
     ], 
     text: "A dynamic text that needs to be displayed to the player."; 
    } 
}; 
1

使用JSONKit。它的更新和更好的版本SBJson