Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios JSON解析后,表视图中的字符串打印错误_Ios_Objective C_Json - Fatal编程技术网

Ios JSON解析后,表视图中的字符串打印错误

Ios JSON解析后,表视图中的字符串打印错误,ios,objective-c,json,Ios,Objective C,Json,在我的一个应用程序中,我解析来自本地主机的一些数据,并将其打印到表视图中。要获取数据,用户首先使用警报视图登录。然后使用输入的用户id获取我使用JSON解析的数据 这个问题肯定有一个非常简单的解决方案,但我似乎无法解决它。问题是,当我打印数据时,字符串的格式如下: (“字符串”) 但我想要它,所以它只是说:字符串 在表视图中。以下是我的解析方法: - (void)updateMyBooks { dispatch_async(dispatch_get_global_queue(DISPA

在我的一个应用程序中,我解析来自本地主机的一些数据,并将其打印到表视图中。要获取数据,用户首先使用警报视图登录。然后使用输入的用户id获取我使用JSON解析的数据

这个问题肯定有一个非常简单的解决方案,但我似乎无法解决它。问题是,当我打印数据时,字符串的格式如下:

(“字符串”)

但我想要它,所以它只是说:字符串

在表视图中。以下是我的解析方法:

 - (void)updateMyBooks
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    // Fetch data on a background thread:

    NSString *authFormatString =
    @"http://localhost:8888/Jineel_lib/bookBorrowed.php?uid=%@";

    NSString *string = [[NSString alloc]initWithFormat:@"%@",UserID];

    NSString *urlString = [NSString stringWithFormat:authFormatString, string];

    NSURL *url = [NSURL URLWithString:urlString];

    NSLog(@"uel is %@", url);

    NSString *contents = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];

    response1 = [contents JSONValue];

    if (contents) {

        // ... Parse JSON response and add objects to newBooksBorrowed ...
        BookName = [[NSString alloc]init];
        DateBorrowed = [[NSString alloc]init];
        BookID = [[NSString alloc]init];
        BookExtended = [[NSString alloc]init];
        BookReturned = [[NSString alloc]init];

        BookName = [response1 valueForKey:@"BookName"];
        BookID = [response1 valueForKey:@"BookID"];
        DateBorrowed = [response1 valueForKey:@"DateBorrowed"];
        BookExtended = [response1 valueForKey:@"Extended"];
        BookReturned = [response1 valueForKey:@"Returned"];

        dispatch_sync(dispatch_get_main_queue(), ^{
            // Update data source array and reload table view.
            [BooksBorrowed addObject:BookName];
            NSLog(@"bookBorrowed array = %@",BooksBorrowed);
            [self.tableView reloadData];
        });
    }
});
}
以下是我在表视图中打印它的方式:

NSString *string = [[NSString alloc] initWithFormat:@"%@",[BooksBorrowed objectAtIndex:indexPath.row]];

NSLog(@"string is %@",string);
cell.textLabel.text = string;
当我在解析过程中使用log时,它会显示为(“字符串”),所以问题出在解析的某个地方,至少我是这么认为的

    NSString *string = [[NSString alloc] initWithFormat:@"%@",[BooksBorrowed objectAtIndex:indexPath.row]];
    string = [string stringByReplacingOccurrencesOfString:@"(" withString:@""];
    string = [string stringByReplacingOccurrencesOfString:@")" withString:@""];
    string = [string stringByReplacingOccurrencesOfString:@"\"" withString:@""];


    NSLog(@"string is %@",string);
    cell.textLabel.text = string;
编辑:

如果在标签中以该格式显示文本,则使用上述代码

如果您在
NSlog
中看到它,那么它就是
NSString
中的
NSArray
。 您需要首先从数组中获取该字符串,然后显示,使用@Martin R建议的代码行

NSString *string = [[BooksBorrowed objectAtIndex:indexPath.row] objectAtIndex:0];
如果

返回类似于“(string)”的内容,那么最可能的原因是

[BooksBorrowed objectAtIndex:indexPath.row]
不是字符串,而是包含字符串的数组。那么,

NSString *string = [[BooksBorrowed objectAtIndex:indexPath.row] objectAtIndex:0];

应该是解决方案。

关于Xcode的问题在哪里?我用苹果ios编码语言的xocde做这件事,因此Xcode标签JSON看起来像什么?耶!非常感谢你,我本应该想到>。这是解决症状,但很难解决问题。所以这个代码要么是错误的,要么是正确的。OP是勇敢的投票者或失败的投票者。此代码将删除所有括号,例如
(“Title(Part 1)”)
将转换为
Title Part 1
。感谢您的解决方案,修正了我的三个问题:)如果你检查了我的其他一些问题,你会意识到我在这个问题上一直有问题,你修正了这个问题,所以现在一切都按照应该的方式解决了,而不是以其他方式建议。。。所以再次感谢你:)
NSString *string = [[BooksBorrowed objectAtIndex:indexPath.row] objectAtIndex:0];