2025-02-17

   這篇日志我會寫一個客戶端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:@"https://……(這裡是服務端的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、最終在控制臺就會輸出解析好的鍵值對應的字符串瞭。

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *