這篇日誌我會寫一個用戶端json解析的小例子,下篇日誌我會寫伺服器端的代碼。
1、進行必要的準備工作。
下載ASIHttpRequest類庫,github上有,https://github.com/pokeb/asi-http-request/
下載json-framework,github上也有,https://github.com/stig/json-framework/
2、將下載的類庫添加到Xcode項目中
3、添加framework
libz.dylib
CFNetwork.framework
SenTestingKit.framework
SystemConfiguration.framework
MobileCoreServices.framework
4、上面的步驟做好之後,下面就是關鍵了。
[plain]
NSURL *url = [NSURL URLWithString:@"http://......(這裡是服務端的url)/Default.aspx"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request startSynchronous];
NSString *response = [request responseString];
NSLog(@"%@",response); //這裡輸出一下,看得到的json字串是否正確
NSMutableArray *data = [response JSONValue]; //這裡得到的json字串裡面含有多個Dictionary
for (NSDictionary *dictionary in data) //對NSMutableArray進行遍曆
{
NSLog(@"%@,%@",[dictionary objectForKey:@"number"],[dictionary objectForKey:@"name"]);
}
5、最終在控制台就會輸出解析好的索引值對應的字串了。