在下面的代码中我解析了下面的web服务,但是当我解析comments数组时,它不会像[]那样考虑数组括号假设括号()所以我不会解析这个数组,怎么回事仍然我没有请帮助我。
请查看以下代码快照,请帮助我。
{
"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
答案 0 :(得分:1)
此代码将对您有所帮助。
- (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);
}
答案 1 :(得分:1)
NSString *urlstring = @"Your URL";
NSURL *url = [NSURL URLWithString:urlstring];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSError *error;
NSURLResponse *response;
NSData *returnData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *returnString=[[NSString alloc]initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"%@",returnString);
SBJSON *json = [SBJSON new];
NSDictionary *dict = [[NSDictionary alloc] initWithDictionary:[json objectWithString:returnString error:nil]];
NSArray *Response_Array = [[NSArray alloc] initWithArray:[dict valueForKey:@"comments"]];