【发布时间】:2014-04-18 05:26:57
【问题描述】:
我正在从 web url 请求动态 json 字符串。
-(void)getDataFromServer{
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://www.domain.com/json/"]];
[request setHTTPMethod:@"GET"];
[request addValue:@"getValues" forHTTPHeaderField:@"METHOD"];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
}
-(void)requestReturnedData:(NSData *)data{ //activated when data is returned
NSDictionary *dictionary = [NSDictionary dictionaryWithJSONData:data];
}
我得到以下错误。
Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)(JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0x977a900 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}
我用json文本文件测试过
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://www.domain.com/jsonfile.json"]];
完美运行。我该如何克服这个问题。
编辑---
我发现,如果 json 中的行数超过 200,则会发生此错误。否则它运行完美。数据大小是否有问题。
【问题讨论】:
-
你的 JSON 是什么样的?您很可能收到一个字符串,或者顶级对象是一个数组。
-
@Inertiatic 顶级对象是一个数组。
标签: ios objective-c nsmutableurlrequest