我有两个问题:问题与返回的JSON数组
1.I想在我的应用程序加载的JSON信息,就是现在 '的NSLog(@ “阵:%@”,self.news);'不显示任何东西,但如果我把它放在'(void)connectionDidFinishLoading:(NSURLConnection *)连接'它的作品,你能告诉我为什么吗?
//making request query string
NSString *requestUrl = [NSString
stringWithFormat:@"%@jsons/json.php?go=product_info&latitude=%g&longitude=%g&identifire=%@&pid=%ld&externalIPAddress=%@&localIPAddress=%@",
BASE_URL,
coordinate.latitude,
coordinate.longitude,
uniqueIdentifier,
(long)self.productId,
[self getIPAddress],
[self getLocalIPAddress]
];
NSURL *url=[NSURL URLWithString:requestUrl];
NSURLRequest *request= [NSURLRequest requestWithURL:url];
NSURLConnection *c=[[NSURLConnection alloc] initWithRequest:request delegate:self];
NSLog(@"Array: %@", self.news);
}
//=========================
-(void)connection: (NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
self.jsonData= [[NSMutableData alloc] init];
}
-(void)connection: (NSURLConnection *)connection didReceiveData:(NSData *)theData{
[self.jsonData appendData:theData];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
self.news=[NSJSONSerialization JSONObjectWithData:self.jsonData options:nil error:nil];
}
-(void)connection: (NSURLConnection *)connection didFailWithError:(NSError *)error{
UIAlertView *errorView=[[UIAlertView alloc] initWithTitle:@"Error" message:@"download could not be compelete" delegate:nil cancelButtonTitle:@"Dissmiss" otherButtonTitles:nil, nil];
[errorView show];
}
2.I总是有警告“不兼容的指针到整数转换发送‘*无效’到类型的参数‘NSJSONreading ...’”的这行代码“self.news = [NSJSONSerialization JSONObjectWithData:自.jsonData选项:nil error:nil];'
self.news是一个数组,我将它改为字典,但我得到了相同的警告消息。
感谢有关NSLog的信息。 我想尝试self.news作为字典,但我如何访问我的数据我曾经使用 'self.topText.text = [[self.news objectAtIndex:0] objectForKey:@“pname”];' 如何在成为字典时将其写入? (对不起,我是初学者) – user2211254
我的信息是一个数组,每个元素都有这样的产品信息: 2013-07-04 18:58:29.301测试[18986:11303] Array:( { cid = 2 ; 图像= “HTTP://xxx/images/loginlogo.png”; 手册= “”; 电影= “http://jplayer.org/video/m4v/Big_Buck_Bunny_Trailer.m4v”; 的pcode = 023942435228; PID = 1; PNAME = “例如产品”; 价格= 12; QR码= “”; 销售= 0; “sale_percent”= 0; 文本=“在这里你可以找到关于该产品的一些额外的信息.... “; } ) – user2211254
在这种情况下,你的根对象是一个数组,所以你应该使用'NSArray'。通常,这就是为什么NSJSONSerialization返回一个不透明类型的原因,因为JSON根对象取决于从请求中获取的JSON数据的格式。 – HepaKKes