- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的 uitableview 加载正常,但是,我得到了这些结果,并且我的 uitableview 上得到了空白数据。我究竟做错了什么?谢谢你的帮助。我似乎在字典或数组方面遇到了问题,但我很困惑在哪里更改代码。
这是我的 json 结果:
{"assistant":[{"assistant":"Joe Black"},{"assistant":"Mary White"}]}
调试结果:
2013-03-27 15:10:38.132 testJson[3833:c07] its probably a dictionary
2013-03-27 15:10:38.133 testJson[3833:c07] jsonDictionary - (null)
2013-03-27 15:10:38.134 testJson[3833:c07] string is <7b226173 73697374 616e7422
3a5b7b22 61737369 7374616e 74223a22 52696320 446f6e61 746f227d 2c7b2261 73736973
74616e74 223a2252 69632044 6f6e6174 6f227d5d 7d>
2013-03-27 15:10:38.134 testJson[3833:c07] nil
2013-03-27 15:10:38.134 testJson[3833:c07] Is mutable? 1
2013-03-27 15:10:38.135 testJson[3833:c07] Is mutable? 1
2013-03-27 15:10:38.135 testJson[3833:c07] this is your datesArray (
)
这是我的.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController <UITextFieldDelegate, UITableViewDelegate,
UITableViewDataSource> {
NSArray *json;
}
@property (nonatomic, retain) NSArray *json;
@property (strong, nonatomic) IBOutlet UITableView *tableView;
@end
这是我的.m
- (void)viewDidLoad
{
[super viewDidLoad];
dispatch_async(kBgQueue, ^{
NSData* data = [NSData dataWithContentsOfURL:
kLatestKivaLoansURL];
[self performSelectorOnMainThread:@selector(fetchedData:)
withObject:data waitUntilDone:YES];
});
}
- (void)fetchedData:(NSData *)responseData {
NSError* error;
self.json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions
error:&error];
if ([json isKindOfClass:[NSArray class]]) {
NSLog(@"its an array!");
NSArray *jsonArray = (NSArray *)json;
NSLog(@"jsonArray - %@",jsonArray);
}
else {
NSLog(@"its probably a dictionary");
NSDictionary *jsonDictionary = (NSDictionary *)json;
NSLog(@"jsonDictionary - %@",jsonDictionary);
}
NSLog(@"string is %@", responseData);
if ([json isKindOfClass:[NSArray class]]) {
NSLog(@"its an array");
}
if ([json isKindOfClass:[NSDictionary class]]) {
NSLog(@"its a dictionary");
}
if ([json isKindOfClass:[NSString class]]) {
NSLog(@"its a string");
}
if ([json isKindOfClass:[NSNumber class]]) {
NSLog(@"its a number");
}
if ([json isKindOfClass:[NSNull class]]) {
NSLog(@"its a null");
}
if (json == nil){
NSLog(@"nil");
}
NSError *parseError;
NSMutableArray *listOfObjects = [NSJSONSerialization JSONObjectWithData:[@"[]"
dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers
error:&parseError];
NSLog(@"Is mutable? %hhi", [listOfObjects isKindOfClass:[NSMutableArray class]]);
listOfObjects = [NSJSONSerialization JSONObjectWithData:[@"[[],{}]"
dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers
error:&parseError];
NSLog(@"Is mutable? %hhi", [listOfObjects isKindOfClass:[NSMutableArray class]]);
NSMutableArray *datesArray = [[NSMutableArray alloc] init];
for (NSDictionary *tempDict in json){
[datesArray addObject:[tempDict objectForKey:@"assistant"]];
}
NSLog(@"this is your datesArray %@", datesArray);
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return self.json.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [[json objectAtIndex:indexPath.row] objectForKey:@"assistant"];
return cell;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
最佳答案
什么是 self.json?您分配它,然后对它进行检查,然后在此处将其视为数组。
[[json objectAtIndex:indexPath.row] objectForKey:@"assistant"]
如果你希望它是一个数组,那么创建一个数组,检查返回的 json 是否是一个数组,将你的数组设置为 json 数组并在你的 TableView 中使用你的数组。
下面是我如何处理这个问题的示例,我使用自己的帮助器类,因此忽略该部分,但请注意我如何检查它是否是一个数组,然后将其分配给我正在等待数据的 NSArray。然后,我在 TableView 中使用该 NSArray 并从中加载数据。
-(void)downloadFinished:(id)sender {
if ([sender isKindOfClass:[JsonHelper class]]) {
JsonHelper *jsonHelper = (JsonHelper *)sender;
NSData *data = [[NSData alloc] initWithData:jsonHelper.receivedData];
NSDictionary *jsonReturn=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
NSLog(@"History Return:%@",jsonReturn);
if ([jsonReturn objectForKey:@"calls"]) {
if ([[jsonReturn objectForKey:@"calls"]isKindOfClass:[NSArray class]]) {
historyArray = [jsonReturn objectForKey:@"calls"];
NSLog(@"History Array:%@",historyArray);
[historyTableView reloadData];
} else {
// Error stuff goes here it was not an array.
}
}
}
在我的 TableView 方法中,我只引用数组
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [historyArray count];
}
等等......
关于iphone - NSJSONSerialization 空白 uitableview 和 null 结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15668168/
我正在使用NSJSONSerialization将json字符串转换为NSDictionaray JSON字符串是 {"bid":88.667,"ask":88.704} 在NSJSONSeriali
我正在使用 iOS 5 新功能来解析 JSON,但我不知道为什么我没有得到任何键值对。 “aStr”(数据的字符串表示形式)将正确的 JSON 放在输出窗口上,但我在“dicData”中什么也没有得到
我正在尝试将 JSON 文件解析为 NSArray,对于正数来说一切都很好。然而,该 JSON 文件中的所有负整数都会生成高数字,例如“[11] __NSCFNumber * (long)720575
我有一个NSJSONSerialization JSONObjectWithData,我在其中传递了NSData对象。结果,我得到一个NSString而不是NSDictionary。我的完整行是: N
在以下用于解析JSON的代码中,当对象碰巧不是NSArray或NSDictionary时,将使用NSJSONReadingAllowFragments。 下面的代码可以正常工作并输出32。但是如果将3
好的,我正在使用以下代码获取 JSON 文件 - (void)dataRequest { NSURL *dataURL = [NSURL URLWithString:@"link to JSO
我有一些看起来像这样的代码: NSDictionary* json = [NSJSONSerialization JSONObjectWithData:data
我在尝试从 URL 解析 JSON 时遇到错误。 代码如下: override func viewDidLoad() { super.viewDidLoad()
我在 AppDelegate.m 中有这段代码: NSError *error; NSString *filePath = [[NSBundle mainBundle] pathFor
这个问题在这里已经有了答案: NSJSONSerialization and Emoji (2 个答案) 关闭 6 年前。
我正在使用 JSON 来填充带有标记的 mapView。 在方法 -(void)connectionDidFinishLoading:(NSURLConnection *)connection 编译器
我在使用 NSJSONSerialization 创建 NSString 的 NSDictionary 表示(JSON 字符串)时遇到了一些问题。我以前使用过 JSONKit,但由于它在 iOS9 中
请原谅我可能犯的错误,我第一次问这个问题。 阅读本网站上的几个主题后,like this one首先我会尝试使用 describe 方法,但它仍然不起作用@all :-( 我的 .json 文件看起来
我的 iPad 应用程序需要解析一个 10 MB 的 JSON 文件并将其内容保存在 Core Data 中。 考虑到大文件大小和内存使用,您会推荐使用 NSJSONSerialization 还是您
我有一个看起来像这样的字符串:(它派生自一个格式略有错误的解码数据对象,我通过控制台修复并验证了这个输出) {"home":{"breakfast":{"protein":"55","carbs":"
我已经在几个项目中使用过它... [NSJSONSerialization dataWithJSONObject:someObject options:0 error:nil] 但我不知道如何指定无选
我有一本字典,当我登录时它会显示... { Date = "2013-04-30 17:17:18 +0000"; Description = Kb; EventID = "9
在我的 iOS 应用程序中,我在后台线程中做一些工作(使用 performSelectorInBackground)。在此线程中,我使用 NSJSONSerialization 及其类方法来解析 JS
我正在使用 NSJSONSerializer 来解析一些 json 格式的数据。数据包含一些“ü”和“ö”等。我发现它确实返回 nil,如果没有事先使用 NSASCIIStringEncoding 将
我在使用 NSJSONSerialization 从 Met Office Datapoint API 读取 JSON 时遇到问题。 出现以下错误 Error Domain=NSCocoaErrorD
我是一名优秀的程序员,十分优秀!