0

我是 iOS 世界的新手,我在玩从 node.js 服务器中提取数据。我正在寻找一种方法来获取这样的输出:我构建和记录只是为了玩。

    2012-11-16 10:18:31.244 Freshly[4952:11603] key: vendor, value: (
       "vendor_of_1",
       "vendor_of_2",
       "vendore_of_3",
       "vendore_of_4"
    ) 
    2012-11-16 10:18:31.245 Freshly[4952:11603] key: messages, value: (
       message1,
       message2,
       Message3,
       message4
    ) 

我可能做错了,但我认为这是基于希望在 UItable 中命名供应商和消息来填充单元格的最佳方式。

我关闭服务器的是:

     [{"content":"Message close","vendor":{"name":"Starbucks","_id":"509f9148c154050200000001","__v":0,"loc":[22.27977,114.156715]},"_id":"509f9148c154050200000002","__v":0,"tags":["Specials","Drink"],"lifespan":{"start":"2012-11-11T11:41:36.752Z","end":"2012-11-11T12:01:36.752Z"},"loc":[22.27977,114.156715]},{"content":"Message loin","vendor":{"name":"Starbucks","_id":"509f9148c154050200000001","__v":0,"loc":[22.27977,114.156715]},"_id":"509f9148c154050200000003","__v":0,"tags":["Food"],"lifespan":{"start":"2012-11-11T10:11:36.754Z","end":"2012-11-11T12:01:36.754Z"},"loc":[5,5]},{"content":"Message close","vendor":{"name":"Starbucks","_id":"509f918ff89e240200000001","__v":0,"loc":[22.27977,114.156715]},"_id":"509f918ff89e240200000002","__v":0,"tags":["Specials","Drink"],"lifespan":{"start":"2012-11-11T11:42:47.828Z","end":"2012-11-11T12:02:47.828Z"},"loc":[22.27977,114.156715]},{"content":"Message loin","vendor":{"name":"Starbucks","_id":"509f918ff89e240200000001","__v":0,"loc":[22.27977,114.156715]},"_id":"509f918ff89e240200000003","__v":0,"tags":["Food"],"lifespan":{"start":"2012-11-11T10:12:47.830Z","end":"2012-11-11T12:02:47.830Z"},"loc":[5,5]},{"content":"Message close","vendor":{"name":"Starbucks","_id":"509fbb99c888e40200000001","__v":0,"loc":[22.27977,114.156715]},"_id":"509fbb99c888e40200000002","__v":0,"tags":["Specials","Drink"],"lifespan":{"start":"2012-11-11T14:42:09.939Z","end":"2012-11-11T15:02:09.939Z"},"loc":[22.27977,114.156715]},{"content":"Message loin","vendor":{"name":"Starbucks","_id":"509fbb99c888e40200000001","__v":0,"loc":[22.27977,114.156715]},"_id":"509fbb99c888e40200000003","__v":0,"tags":["Food"],"lifespan":{"start":"2012-11-11T13:12:09.941Z","end":"2012-11-11T15:02:09.941Z"},"loc":[5,5]},{"content":"Message close","vendor":{"name":"Starbucks","_id":"509fcafa4550af0200000001","__v":0,"loc":[22.27977,114.156715]},"_id":"509fcafa4550af0200000002","__v":0,"tags":["Specials","Drink"],"lifespan":{"start":"2012-11-11T15:47:46.751Z","end":"2012-11-11T16:07:46.751Z"},"loc":[22.27977,114.156715]},{"content":"Message loin","vendor":{"name":"Starbucks","_id":"509fcafa4550af0200000001","__v":0,"loc":[22.27977,114.156715]},"_id":"509fcafa4550af0200000003","__v":0,"tags":["Food"],"lifespan":{"start":"2012-11-11T14:17:46.753Z","end":"2012-11-11T16:07:46.753Z"},"loc":[5,5]},{"content":"Message loin","vendor":{"name":"Starbucks","_id":"509ff452ed9fdd0200000001","__v":0,"loc":[22.27977,114.156715]},"_id":"509ff452ed9fdd0200000003","__v":0,"tags":["Food"],"lifespan":{"start":"2012-11-11T17:14:10.082Z","end":"2012-11-11T19:04:10.082Z"},"loc":[5,5]},{"content":"Message close","vendor":{"name":"Starbucks","_id":"509ff452ed9fdd0200000001","__v":0,"loc":[22.27977,114.156715]},"_id":"509ff452ed9fdd0200000002","__v":0,"tags":["Specials","Drink"],"lifespan":{"start":"2012-11-11T18:44:10.080Z","end":"2012-11-11T19:04:10.080Z"},"loc":[22.27977,114.156715]},{"content":"Message close","vendor":{"name":"Starbucks","_id":"50a004620deba40200000001","__v":0,"loc":[22.27977,114.156715]},"_id":"50a004620deba40200000002","__v":0,"tags":["Specials","Drink"],"lifespan":{"start":"2012-11-11T19:52:42.412Z","end":"2012-11-11T20:12:42.412Z"},"loc":[22.27977,114.156715]},{"content":"Message loin","vendor":{"name":"Starbucks","_id":"50a004620deba40200000001","__v":0,"loc":[22.27977,114.156715]},"_id":"50a004620deba40200000003","__v":0,"tags":["Food"],"lifespan":{"start":"2012-11-11T18:22:42.413Z","end":"2012-11-11T20:12:42.413Z"},"loc":[5,5]},{"content":"Message close","vendor":{"name":"Starbucks","_id":"50a39413be8c8b0200000001","__v":0,"loc":[22.27977,114.156715]},"_id":"50a39413be8c8b0200000002","__v":0,"tags":["Specials","Drink"],"lifespan":{"start":"2012-11-14T12:42:35.885Z","end":"2012-11-14T13:02:35.885Z"},"loc":[22.27977,114.156715]},{"content":"Message loin","vendor":{"name":"Starbucks","_id":"50a39413be8c8b0200000001","__v":0,"loc":[22.27977,114.156715]},"_id":"50a39413be8c8b0200000003","__v":0,"tags":["Food"],"lifespan":{"start":"2012-11-14T11:12:35.894Z","end":"2012-11-14T13:02:35.894Z"},"loc":[5,5]},{"content":"Message close","vendor":{"name":"Starbucks","_id":"50a3a59762628e0200000001","__v":0,"loc":[22.27977,114.156715]},"_id":"50a3a59762628e0200000002","__v":0,"tags":["Specials","Drink"],"lifespan":{"start":"2012-11-14T13:57:19.647Z","end":"2012-11-14T14:17:19.647Z"},"loc":[22.27977,114.156715]},{"content":"Message loin","vendor":{"name":"Starbucks","_id":"50a3a59762628e0200000001","__v":0,"loc":[22.27977,114.156715]},"_id":"50a3a59762628e0200000003","__v":0,"tags":["Food"],"lifespan":{"start":"2012-11-14T12:27:19.649Z","end":"2012-11-14T14:17:19.649Z"},"loc":[5,5]},{"content":"Message close","vendor":{"name":"Starbucks","_id":"50a3b5f7f8eb510200000001","__v":0,"loc":[22.27977,114.156715]},"_id":"50a3b5f7f8eb510200000002","__v":0,"tags":["Specials","Drink"],"lifespan":{"start":"2012-11-14T15:07:11.412Z","end":"2012-11-14T15:27:11.412Z"},"loc":[22.27977,114.156715]},{"content":"Message loin","vendor":{"name":"Starbucks","_id":"50a3b5f7f8eb510200000001","__v":0,"loc":[22.27977,114.156715]},"_id":"50a3b5f7f8eb510200000003","__v":0,"tags":["Food"],"lifespan":{"start":"2012-11-14T13:37:11.413Z","end":"2012-11-14T15:27:11.413Z"},"loc":[5,5]},{"content":"Message close","vendor":{"name":"Starbucks","_id":"50a3c514273d070200000001","__v":0,"loc":[22.27977,114.156715]},"_id":"50a3c514273d070200000002","__v":0,"tags":["Specials","Drink"],"lifespan":{"start":"2012-11-14T16:11:40.930Z","end":"2012-11-14T16:31:40.930Z"},"loc":[22.27977,114.156715]},{"content":"Message loin","vendor":{"name":"Starbucks","_id":"50a3c514273d070200000001","__v":0,"loc":[22.27977,114.156715]},"_id":"50a3c514273d070200000003","__v":0,"tags":["Food"],"lifespan":{"start":"2012-11-14T14:41:40.932Z","end":"2012-11-14T16:31:40.932Z"},"loc":[5,5]},{"content":"Message close","vendor":{"name":"Starbucks","_id":"50a3d5e7c947720200000001","__v":0,"loc":[22.27977,114.156715]},"_id":"50a3d5e7c947720200000002","__v":0,"tags":["Specials","Drink"],"lifespan":{"start":"2012-11-14T17:23:27.443Z","end":"2012-11-14T17:43:27.443Z"},"loc":[22.27977,114.156715]},{"content":"Message loin","vendor":{"name":"Starbucks","_id":"50a3d5e7c947720200000001","__v":0,"loc":[22.27977,114.156715]},"_id":"50a3d5e7c947720200000003","__v":0,"tags":["Food"],"lifespan":{"start":"2012-11-14T15:53:27.445Z","end":"2012-11-14T17:43:27.445Z"},"loc":[5,5]},{"content":"Message loin","vendor":{"name":"Starbucks","_id":"50a56aa80253600200000001","__v":0,"loc":[22.27977,114.156715]},"_id":"50a56aa80253600200000003","__v":0,"tags":["Food"],"lifespan":{"start":"2012-11-15T20:40:24.971Z","end":"2012-11-15T22:30:24.971Z"},"loc":[5,5]},{"content":"Message close","vendor":{"name":"Starbucks","_id":"50a56aa80253600200000001","__v":0,"loc":[22.27977,114.156715]},"_id":"50a56aa80253600200000002","__v":0,"tags":["Specials","Drink"],"lifespan":{"start":"2012-11-15T22:10:24.970Z","end":"2012-11-15T22:30:24.970Z"},"loc":[22.27977,114.156715]},{"content":"Message close","vendor":{"name":"Starbucks","_id":"50a5860700033d0200000001","__v":0,"loc":[22.27977,114.156715]},"_id":"50a5860700033d0200000002","__v":0,"tags":["Specials","Drink"],"lifespan":{"start":"2012-11-16T00:07:11.060Z","end":"2012-11-16T00:27:11.060Z"},"loc":[22.27977,114.156715]},{"content":"Message loin","vendor":{"name":"Starbucks","_id":"50a5860700033d0200000001","__v":0,"loc":[22.27977,114.156715]},"_id":"50a5860700033d0200000003","__v":0,"tags":["Food"],"lifespan":{"start":"2012-11-15T22:37:11.067Z","end":"2012-11-16T00:27:11.067Z"},"loc":[5,5]},{"content":"Message close","vendor":{"name":"Starbucks","_id":"50a5f96f43fc550200000001","__v":0,"loc":[22.27977,114.156715]},"_id":"50a5f96f43fc550200000002","__v":0,"tags":["Specials","Drink"],"lifespan":{"start":"2012-11-16T08:19:35.698Z","end":"2012-11-16T08:39:35.698Z"},"loc":[22.27977,114.156715]},{"content":"Message loin","vendor":{"name":"Starbucks","_id":"50a5f96f43fc550200000001","__v":0,"loc":[22.27977,114.156715]},"_id":"50a5f96f43fc550200000003","__v":0,"tags":["Food"],"lifespan":{"start":"2012-11-16T06:49:35.700Z","end":"2012-11-16T08:39:35.700Z"},"loc":[5,5]},{"content":"Message close","vendor":{"name":"Starbucks","_id":"50a60a38959fc90200000001","__v":0,"loc":[22.27977,114.156715]},"_id":"50a60a38959fc90200000002","__v":0,"tags":["Specials","Drink"],"lifespan":{"start":"2012-11-16T09:31:12.497Z","end":"2012-11-16T09:51:12.497Z"},"loc":[22.27977,114.156715]},{"content":"Message loin","vendor":{"name":"Starbucks","_id":"50a60a38959fc90200000001","__v":0,"loc":[22.27977,114.156715]},"_id":"50a60a38959fc90200000003","__v":0,"tags":["Food"],"lifespan":{"start":"2012-11-16T08:01:12.499Z","end":"2012-11-16T09:51:12.499Z"},"loc":[5,5]}]

我像这样解析:

    NSString *cachedPath = @"/Users/masonedwards/Documents/Developer/Freshly/Freshly/cached_data.txt";

    NSData *data = [NSData dataWithContentsOfURL:url];
    if (data){
        NSError *error;
        self.updates  =  [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
    NSLog(@"udpates %@", self.updates);
    }else {
         self.updates = [NSDictionary dictionaryWithContentsOfFile:cachedPath];
    }
    NSLog(@"The Data is %@", self.updates);

想要得到这样的细胞

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"updatesCell";
    UITableViewCell *cell = [self.updatesTV dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
     }  

    NSString *message = [self.updates objectForKey:@"content"];  
    NSString *vendor =  [[self.updates objectForKey:@"vendor"] objectAtIndex:indexPath.row];

   cell.textLabel.text = vendor;
   cell.detailTextLabel.text = message;

   return cell;
}

我可能完全错了,第一个应用程序。但是任何形式的帮助都会很棒。

4

2 回答 2

0

您应该寻找的关键字是 NSJSONSerialization:这是 Google 显示的内容:https ://www.google.de/search?q=nsjsonserialization&aq=0&oq=NSJSONSeri&aqs=chrome.0.0j57j0l2j62l2.4211&sugexp=chrome,mod=9&sourceid=铬&ie=UTF-8

苹果为此任务有自己的类:http: //developer.apple.com/library/ios/#documentation/Foundation/Reference/NSJSONSerialization_Class/Reference/Reference.html

阅读此内容,您应该进入下一步。

于 2012-11-16T10:53:59.847 回答
0

试试下面的代码希望它有帮助。

 NSData *jsonData = [NSData dataWithContentsOfURL:url];


            NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:&error];

          self.allData= [jsonArray mutableCopy];  

//Then Access All the data individually

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"updatesCell";
    UITableViewCell *cell = [self.updatesTV dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
     }  

NSString *message =[ [self.allData  objectAtIndex:indexPath.row] objectForKey:@"content"]];
NSDictionary *jsonDictionary =[ [self.allData  objectAtIndex:indexPath.row] objectForKey:@"vendor"]];

   NSString *vendor = [jsonDictionary  objectForKey:@"name"];

   cell.textLabel.text = vendor;
   cell.detailTextLabel.text = message;

   return cell;
}
于 2012-11-16T11:18:25.597 回答