如何将json响应放入表格单元格中

时间:2012-09-06 04:04:59

标签: json uitableview ios5

我是ios dev的新手,到目前为止,我几乎一直都在苦苦挣扎。真的非常令人沮丧。

我已将评论/ 代码崩溃在这里 /以指示我的应用流程停止的位置。我用

请求json数据
NSData* data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://192.168.2.4/iRestaurant/users"]];
    [self performSelectorOnMainThread:@selector(fetchedData:)
                           withObject:data waitUntilDone:YES];

fetchedData的实现

- (void)fetchedData:(NSData *)responseData {
//parse out the json data
NSError* error;
NSDictionary* json = [NSJSONSerialization
                      JSONObjectWithData:responseData //1

                      options:kNilOptions
                      error:&error];
//NSLog(@"%@", json);
NSArray* latestLoans = [json objectForKey:@"Users"]; //2
NSEnumerator* enumerator = [latestLoans objectEnumerator];
id element;

  while(element = [enumerator nextObject]) {
    Result *fetchedResults = [Result init];
    /*crashes here*/
    fetchedResults.name = [[element objectForKey:@"User"] objectForKey:@"name"]; 
    fetchedResults.email = [[element objectForKey:@"User"] objectForKey:@"name"];
    [results addObject:fetchedResults];objectForKey:@"name"]);
  }
}

结果是一个自定义对象,是NSObject的子类。我导入了Result.h。我还在当前文件的声明文件中声明了NSMutableArray *结果。我正在将对象推送到结果。 Result的头文件看起来像

@interface Result : NSObject

@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *email;
@property (nonatomic, assign) int ID;

@end

我将实现cellForRowAtIndexPath并将结果的内容推送到可重用的单元格中。

我的JSON回复是

{"Users":
    [{"User":
        {"id":"1",
         "name":"Kishor kundan",
         "email":"kis@kun.ca",
         "password":"asdfasdf",
         "fb_id":"1234444"
        }
     },
     {"User":         
        {"id":"2",
         "name":"adsfasdf",
         "email":"asdfasdf@asdf.asdf",
         "password":"asdfasdf",
         "fb_id":"123123"
     }
    }]
}

1 个答案:

答案 0 :(得分:0)

由于找不到对象,代码崩溃了。我的代码引用了一个不存在的位置。我从我的其他question

中提出了@ btype的建议