2

在下面的代码中,我解析了以下 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

4

2 回答 2

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);
  }
于 2012-05-07T07:58:14.463 回答
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"]];
于 2012-05-07T13:34:14.727 回答