簡體   English   中英

IOS 4.3如何從Web服務中正確解析Json響應並將其綁定到UITABLEVIEW

[英]IOS 4.3 How to properly Parse Json response from a Web-Service and bind it to UITABLEVIEW

我是iOS開發的新手。 我要做的是解析JSON Web服務(參見下面的示例輸出)。 我想將所述輸出綁定到UITableView。 你能告訴我如何做到這一點的示例代碼。 我正在使用帶有4.3 sdk的Xcode 3。 非常感謝你!

[{
    "event_id": "30",
    "bar_name": "Area 05 Superclub",
    "event_name": "test10",
    "date": "Dec 05, 2012 10:00 AM"
}, {
    "event_id": "27",
    "bar_name": "Area 05 Superclub",
    "event_name": "test7",
    "date": "Dec 02, 2012 10:00 AM"
}, {
    "event_id": "28",
    "bar_name": "Area 05 Superclub",
    "event_name": "test8",
    "date": "Dec 03, 2012 10:00 AM"
}, {
    "event_id": "29",
    "bar_name": "Area 05 Superclub",
    "event_name": "test9",
    "date": "Dec 04, 2012 10:00 AM"
}]

好的,這里可能是初始代碼,(關心Tim Stullich,謝謝男人!)。 我能夠從webservice中提取da數據。 我的下一個問題是如何將它綁定到UITableView。 希望你能再次幫助我。

-(void)loadData{
    // Create new SBJSON parser object
    SBJsonParser *parser = [[SBJsonParser alloc] init];
    // Prepare URL request to download statuses from Twitter
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http:xxx/gl_getEventsInformation.php"]];

    // Perform request and get JSON back as a NSData object
    NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

    // Get JSON as a NSString from NSData response
    NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];

    // parse the JSON response into an object
    // Here we're using NSArray since we're parsing an array of JSON status objects
    NSArray *statuses = [parser objectWithString:json_string error:nil];

    // Each element in statuses is a single status
    // represented as a NSDictionary
    for (NSDictionary *status in statuses)
    { NSLog(@"%@ - %@ - %@ - %@", [status objectForKey:@"event_id"],[status objectForKey:@"bar_name"],[status objectForKey:@"event_name"],[status objectForKey:@"date"]);}
}

使用IOS5,您可以使用NSJSONSerialization來解析JSON。

NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:NSJSONWritingPrettyPrinted error:&error];

並且您可以執行類似以下操作來從Json Data生成NSArray。

   NSError *e = nil; NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData: data options: NSJSONReadingMutableContainers error: &e];

    if (!jsonArray) 
       {   NSLog(@"Error parsing JSON: %@", e); } 
   else {    for(NSDictionary *item in jsonArray) 
         {
            NSLog(@"Item: %@", item);    
         }
       }

希望這對你有所幫助

您可以使用諸如此類的json庫(TouchJSON)將數據反序列化為可可對象。 一個簡單的工作流程是這樣的:

#import "CJSONDeserializer.h"
...
NSString *jsonString = @"yourJSONHere";
NSData   *jsonData   = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSError  *error      = nil;
NSArray  *jsonArray  = [[CJSONDeserializer deserializer] deserializeAsArray:jsonData error:&error];

PS。 您還可以看到: https//stackoverflow.com/questions/286087/best-json-library-to-use-when-developing-an-iphone-application它可以幫助您確定最適合您需求的內容。

我要做的就是檢查這個鏈接。 基本上你需要的是將你收到的數據解析成NSArray或NSMutableArray,然后實現UITableView使用的所需方法並從那里開始。 如果您需要更多信息,請告訴我們。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM