在下面的代码中,我解析了以下Web服务,但是当我解析注释数组时,它不被视为像[]这样的数组括号,它假设括号()所以我不会解析这个数组,它是如何发生的仍然我没有请帮助我。
请查看以下代码快照并请帮助我。
{
"flag?":false,
"created_at":"2012-04-29T08:20:04Z",
"title":"test",
"user":{
"name":"abc"
},
"comments":[
{
"comment":"Hi",
"created_at":"2012-04-26T07:56:09Z",
"user":{
"name":"abc"
}
},
{
"comment":"hello",
"created_at":"2012-04-27T07:41:29Z",
"user":{
"name":"Mahesh"
}
},
{
"comment":"good",
"created_at":"2012-05-01T07:03:02Z",
"user":{
"name":"admin"
}
}
],
"solution":"test",
"problem":"test"
}
代码片段如下 =: http://pastebin.com/Dg4hjdKA
最佳答案
这段代码会对你有帮助。
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[responseData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[responseData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
[connection release];
self.responseData = nil;
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[connection release];
NSLog(@"Response String= %@",responseString); //response string is ur JSON Array value
NSArray* latestLoans = [(NSDictionary*)[responseString JSONValue] objectForKey:@"comments"];
NSLog(@"Lateste = %@",latestLoans);
[responseString release];
NSDictionary* loan = [latestLoans objectAtIndex:0];
NSString* number1 = [loan objectForKey:@"comment"];
NSString* firstName = [loan objectForKey:@"created_at"];
NSString* emails = [loan objectForKey:@"user"];
NSLog(@"number1= %@, firstName= %@ Emails= %@",number1,firstName,emails);
}
关于iphone - iPhone中的Json数组解析问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10478563/