9

我使用NSJSONSerialization'sJSONObjectWithData:data options: error:来解析从服务器返回的 JSON 数据。

现在对于我使用的选项参数:NSJSONReadingAllowFragments. 您可以在下面查看实际的 JSON(我认为问题出在哪里)。

我得到的错误信息是:

错误域 = NSCocoaErrorDomain 代码 = 3840“操作无法完成。(可可错误 3840。)”(字符 0 周围的值无效。) UserInfo = 0x6895da0 {NSDebugDescription = 字符 0 周围的值无效。}

知道如何解决吗?

JSON =

{"name":"Johan Appleseed",
"email":"j.appleseed@emuze.co",
"phone":"+4121876003",
"accounts":{
    "facebook":[true,1125],
    "twitter":[false,null],
    "homepage":[true,"http:\/\/johnAplleseed.com\/index.html"]}}
4

6 回答 6

12

可能你有一些你看不到的不可打印的字符。尝试这个:

NSData *jsonData = ...
const unsigned char *ptr = [data bytes];

for(int i=0; i<[data length]; ++i) {
  unsigned char c = *ptr++;
  NSLog(@"char=%c hex=%x", c, c);
}

验证您在数据的开头或结尾没有不可打印的字符。

编辑:澄清一下,只需在您的 JSON 字典上运行上述内容 - 无法解析的字典。

于 2012-08-22T11:21:36.870 回答
2

我实际上发现问题在于从 URL 返回的事实是一个 HTML 页面,所有这些 html、head 和 body 标记都围绕着实际响应,因此无法解析。这是关于如何从响应中删除 HTML 标记(在它已更改为字符串之后)的一个很好的问答:Remove HTML Tags from an NSString on the iPhone

于 2012-08-25T08:46:31.150 回答
1

我有一段时间遇到同样的问题,我只是想如果我从网页中提取数据,比如说 PHP 页面,那么该页面中不应该有任何 HTML 标签。所以像这样的结构:

<html>
<body>
<?php

?>
</body>
</html>

会毁了你的结果。把它变成:

<?php

?> 

为我工作。

于 2014-03-24T05:45:10.967 回答
0

一种方法是,您可以通过 post 请求解析 jsondata

 -(void)callWebserviceList
 {
spinner.hidden=NO;

NSString *bodyData = @"code_request=monuments_list&asi_id=1";

NSMutableURLRequest *postRequest = [NSMutableURLRequest requestWithURL:[NSURL   URLWithString:@"http://asicircles.com/server_sync.php"]];

// Set the request's content type to application/x-www-form-urlencoded
[postRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];

// Designate the request a POST request and specify its body data
[postRequest setHTTPMethod:@"POST"];
[postRequest setHTTPBody:[NSData dataWithBytes:[bodyData UTF8String] length:strlen([bodyData UTF8String])]];

connection1 = [NSURLConnection connectionWithRequest:postRequest delegate:self];

if(connection1 !=nil)
{
    ////NSLog(@"%@",postRequest);
}

}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {

if ([connection isEqual:connection1 ])
{
     [responseData setLength:0];

}else if ([connection isEqual:connection2 ])
{
     [responseData1 setLength:0];
}

} - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {

    if ([connection isEqual:connection1 ])
  {
    [responseData appendData:data];

}else if ([connection isEqual:connection2 ])
{
     [responseData1 appendData:data];
}


//**check here for responseData & also data**

}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"%@",[NSString stringWithFormat:@"Connection failed: %@", [error description]]);
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
if ([connection isEqual:connection1 ])
{
    spinner.hidden=YES;
    NSError *error;

    NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];

    NSMutableArray *arrdata=[json objectForKey:@"message"];
    NSLog(@"code is%@", json);

    for (int i=0; i< arrdata.count; i++)
    {
        [arrDetails addObject:[[arrdata objectAtIndex:i]objectForKey:@"details"]];
        [arrImageUrl addObject:[[arrdata objectAtIndex:i]objectForKey:@"image"]];
        [arrLat addObject:[[arrdata objectAtIndex:i]objectForKey:@"lat"]];
        [arrLongi addObject:[[arrdata objectAtIndex:i]objectForKey:@"longi"]];
        [arrName addObject:[[arrdata objectAtIndex:i]objectForKey:@"name"]];
        [arrLoc addObject:[[arrdata objectAtIndex:i]objectForKey:@"location"]];
        [arrID addObject:[[arrdata objectAtIndex:i]objectForKey:@"id"]];

        NSLog(@"code is%@",[[arrdata objectAtIndex:i]objectForKey:@"details"]);
        NSLog(@"code is%@",[arrImageUrl objectAtIndex:i]);

    }



    if (arrName.count > 0)
    {
        [self addscrollView];
    }



}else if ([connection isEqual:connection2 ])
{

}


}
于 2014-02-05T06:08:02.300 回答
0

已经有一段时间了,但打印数据的更简单方法是:

NSLog(@"%@", [[NSString alloc] initWithData:数据编码:NSUTF8StringEncoding]);

于 2013-11-12T22:59:55.567 回答
0

您还可以在 ios 中按块点击 json 数据的 url

 #define kBgQueue dispatch_get_global_queue(
 DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) //1
#define kLatestKivaLoansURL [NSURL URLWithString: 
 @"http://api.kivaws.org/v1/loans/search.json?status=fundraising"] //2

我们需要做的第一件事是从网上下载 JSON 数据。幸运的是,使用 GCD,我们可以在一行代码中做到这一点!将以下内容添加到 ViewController.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 {
//parse out the json data
NSError* error;
NSDictionary* json = [NSJSONSerialization 
    JSONObjectWithData:responseData //1

    options:kNilOptions 
    error:&error];

NSArray* latestLoans = [json objectForKey:@"loans"]; //2

NSLog(@"loans: %@", latestLoans); //3
}
于 2014-02-05T06:18:36.403 回答