2012-05-07 19 views
2

次のコードでは、次のWebサービスを解析しますが、コメント配列を解析するとき、[]のような配列の括弧は考慮しません。この配列を解析しません。起こるまだ私は私を助けてくれない。iphoneでのJson Array解析の問題

次のコードを見て、私を助けてください。

{ 
    "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

[可能性の重複](http://stackoverflow.com/questions/10443425/xcode-parsing-json-array) – akk

+0

ペーストビンURLはまだ空のページを示しています... – akk

答えて

1

このコードはuのに役立ちます。

- (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); 
    } 
+0

それは働いていない申し訳ありません。 ....同じo/pは私に – Pradeep

+0

あなたがpastebin.comを使用してURの完全なコードを投稿することができますか? – akk

+0

はい....コードhttp://pastebin.com/Dg4hjdKAを投稿します – Pradeep

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"]];