objective c从url请求解析json [英] objective c parse json from url request

查看:118
本文介绍了objective c从url请求解析json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试解析从位于以下位置的api请求的json字符串:
http://www.physics.leidenuniv.nl/json/news.php



但是,我在解析这个json时遇到了问题。
我收到以下错误:
字符串解析期间文件意外结束



I已经找了几个小时,但我找不到这个问题的答案。



我的代码片段:



在我的viewDidLoad中:

  NSURLRequest * request = [NSURLRequest requestWithURL:
[NSURL URLWithString:@http:// www.physics.leidenuniv.nl/json/news.php]];

[[NSURLConnection alloc] initWithRequest:request delegate:self];

代表:

   - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
NSMutableData * responseData = [[NSMutableData alloc] init];
[responseData appendData:data];

NSString * responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSError * e = nil;
NSData * jsonData = [responseString dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary * JSON = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:& e];
}

任何人都知道这个问题的答案,所以我可以解析json数据吗?这是这样的:

   - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{
//将新数据附加到receivedData。
// receivedData是在别处声明的实例变量。

[responseData appendData:data];
}


- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString * responseString = [[NSString alloc] initWithData:responseData encoding :NSUTF8StringEncoding];
NSError * e = nil;
NSData * jsonData = [responseString dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary * JSON = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:& e];

}


I am trying to parse a json string requested from an api located at: http://www.physics.leidenuniv.nl/json/news.php

However, i am having trouble parsing this json. I get the following error: Unexpected end of file during string parse

I have looked for hours, but I can not find an answer to this problem.

My code snippet:

In my viewDidLoad:

NSURLRequest *request = [NSURLRequest requestWithURL:
                         [NSURL URLWithString:@"http://www.physics.leidenuniv.nl/json/news.php"]];

[[NSURLConnection alloc] initWithRequest:request delegate:self];

The delegate:

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
NSMutableData *responseData = [[NSMutableData alloc] init];
[responseData appendData:data];

NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSError *e = nil;
NSData *jsonData = [responseString dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData:jsonData options: NSJSONReadingMutableContainers error: &e];
}

Anybody know an answer to this problem so i can parse the json data?

解决方案

Do this way:

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    // Append the new data to receivedData.
    // receivedData is an instance variable declared elsewhere.

    [responseData appendData:data];
}


- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSError *e = nil;
NSData *jsonData = [responseString dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData:jsonData options: NSJSONReadingMutableContainers error: &e];

}

这篇关于objective c从url请求解析json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆