得到崩溃而没有得到json数据

时间:2015-11-23 08:43:25

标签: ios objective-c json

我用来从这个url api获取数据。但是我在我的控制台中检查了它但是它崩溃了。它喜欢获取电影列表并在表格视图中显示。我用它来检查我是否正在以正确的方式进行或以错误的方式。

这是我的代码:

编辑:

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSString *authUrl =@"http://www.omdbapi.com/?s=Game%20of%20Thrones&Season=1";

    NSString *str1 = [authUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    NSURL *url = [NSURL URLWithString:str1];
    NSData * data=[NSData dataWithContentsOfURL:url];

    NSError * error;
    NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingMutableContainers error: &error];
    NSLog(@"json%@",json);
}

2 个答案:

答案 0 :(得分:1)

我有你的解决方案。

只需在代码中替换我的代码。

在控制台中你看到了结果..

 NSString *authUrl =@"http://www.omdbapi.com/?s=Game of Thrones&Season=1";

NSString *str1 = [authUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

NSURL *url = [NSURL URLWithString:str1];
NSData * data=[NSData dataWithContentsOfURL:url];

NSError * error;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingMutableContainers error: &error];
NSLog(@"json%@",json);

在控制台中

json{
    Search =     (
                {
            Poster = "http://ia.media-imdb.com/images/M/MV5BNTgxOTI4NzY2M15BMl5BanBnXkFtZTgwMjY3MTM2NDE@._V1_SX300.jpg";
            Title = "Game of Thrones";
            Type = series;
            Year = "2011\U2013";
            imdbID = tt0944947;
        },
                {
            Poster = "http://ia.media-imdb.com/images/M/MV5BMjE5NTk5NDg3OV5BMl5BanBnXkFtZTgwNDExNzg2MDE@._V1_SX300.jpg";
            Title = "Game of Thrones";
            Type = game;
            Year = 2012;
            imdbID = tt2231444;
        },
                {
            Poster = "N/A";
            Title = "Game of Thrones: A Telltale Games Series";
            Type = game;
            Year = 2014;
            imdbID = tt3391176;
        },
                {
            Poster = "http://ia.media-imdb.com/images/M/MV5BMTU1MzU2MDE4MV5BMl5BanBnXkFtZTgwNTc3NzA2MDE@._V1_SX300.jpg";
            Title = "Game of Thrones: Season 2 - Character Profiles";
            Type = movie;
            Year = 2013;
            imdbID = tt2653342;
        },
                {
            Poster = "N/A";
            Title = "Game of Thrones: A Day in the Life";
            Type = movie;
            Year = 2015;
            imdbID = tt4437700;
        },
                {
            Poster = "http://ia.media-imdb.com/images/M/MV5BMTc5MjM4OTkzMV5BMl5BanBnXkFtZTgwMzc3NzA2MDE@._V1_SX300.jpg";
            Title = "Game of Thrones: Season 2 - Invitation to the Set";
            Type = movie;
            Year = 2012;
            imdbID = tt2653340;
        },
                {
            Poster = "http://ia.media-imdb.com/images/M/MV5BMTYyODg1MjYzMV5BMl5BanBnXkFtZTgwOTc3NzA2MDE@._V1_SX300.jpg";
            Title = "Game of Thrones: Costumes";
            Type = movie;
            Year = 2011;
            imdbID = tt2653350;
        },
                {
            Poster = "N/A";
            Title = "Game of Thrones: You Win or You Die - Inside the HBO Series";
            Type = movie;
            Year = 2012;
            imdbID = tt2972984;
        },
                {
            Poster = "http://ia.media-imdb.com/images/M/MV5BMTQ1NzY5NDE1Ml5BMl5BanBnXkFtZTgwODc3NzA2MDE@._V1_SX300.jpg";
            Title = "Game of Thrones: Season 2 - How to Be an Extra";
            Type = movie;
            Year = 2012;
            imdbID = tt2653338;
        },
                {
            Poster = "http://ia.media-imdb.com/images/M/MV5BMTgzNjYxNjY3NF5BMl5BanBnXkFtZTgwMTg3NzA2MDE@._V1_SX300.jpg";
            Title = "Game of Thrones: Inside the Night's Watch";
            Type = movie;
            Year = 2011;
            imdbID = tt2653352;
        }
    );
}

答案 1 :(得分:1)

如果您想使用GET从服务器获取响应,您可以尝试以下方法

//First give your URL here
NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:[NSURL  URLWithString:@"http://www.omdbapi.com/?s=Game%20of%20Thrones&Season=1"]];

//Set GET method for getting Response
[request setHTTPMethod:@"GET"];

//setValue and HTTPHeaderField here
[request setValue:@"application/json;charset=UTF-8" forHTTPHeaderField:@"content-type"];

NSError *err;

NSURLResponse *response;

NSData *responseData = [NSURLConnection sendSynchronousRequest:request   returningResponse:&response error:&err];

//Now you need to check response.Once you get the response,copy that and paste in ONLINE JSON VIEWER(google this or type online json viewer in google). 

//Paste the response and click the Viewer.

//Set whether it is DICTIONARY(if it starts with {}json) or ARRAY(if it starts with []json),depends upon the json result.

NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:responseData options: NSJSONReadingMutableContainers error: &err];

           //OR           

NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:responseData options: NSJSONReadingMutableContainers error: &err];