2011-06-20 6 views
0

: -JSONの解析+ iphone

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    NSLog(@"viewdidload"); 
    responseData = [[NSMutableData data] retain]; 
    NSURLRequest *request = [NSURLRequest requestWithURL: 
          [NSURL URLWithString:@"https://maps.googleapis.com/maps/api/place/search/json?location=-33.8670522,151.1957362&radius=500&types=food&name=harbour&sensor=false&key=AIzaSyAbgGH36jnyow0MbJNP4g6INkMXqgKFfHk"]]; 
    [[NSURLConnection alloc] initWithRequest:request delegate:self]; 
} 

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { 
    NSLog(@"didReceiveResponse"); 
    [responseData setLength:0]; 
} 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {   
    [responseData appendData:data]; 
} 

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {  
    NSLog(@"didFailWithError"); 
    label.text = [NSString stringWithFormat:@"Connection failed: %@", 
              [error description]]; 
} 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection { 
    NSLog(@"connectionDidFinishLoading"); 
    [connection release]; 
} 

私を導いてください、それは本当です。 そして、私たちがXMLで行う正確なタグのデータをどのように取得することができますか?

答えて

1

...と、ここでは、iPhone上で行うの方法は次のとおりです。

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 

NSDictionary *feed = responseData; 
// get the array of "results" from the feed and cast to NSArray 
NSArray *results= (NSArray *)[feed valueForKey:@"results"]; 

// loop over all the results objects and print their names 
int ndx; 
NSDictionary *result; 
for (ndx = 0; ndx < results.count; ndx++) { 
    NSDictionary *result = (NSDictionary *)[results objectAtIndex:ndx]; 
    NSLog(@"This is the name of this result: %@", [resultvalueForKey:@"name"]); 
} 
+0

おかげinbuildされるJSON解析URL文字列へのJSON接続するための簡単なデモです –

1

あなたはここにresponseDataを取得します。あなたはあなたの応答が配列または辞書にある場合は、簡単な書き込み

[responseString JSONValue]; 

、NSStringの中でそれを変換する必要があなたの応答が辞書形式であるならば、あなたはaccordingly.Likeでこれをキャッチしているのは、その後

NSDictionary *tempDict = [responseString JSONValue]; 
を書きます

これがあなたを助けてくれることを願っています。

1

聞くには、あなたがこのためにJSONのframworkを追加する必要がありますが、今IOSで5 JSONをXcodeで返信用

- (void)viewDidLoad 
{ 
[super viewDidLoad]; 
[self MethodCalling:@"GET" Body:@"" ValueForPut:@""]; 
} 
-(void)MethodCalling:(NSString *)HttpMethod Body:(NSString *)BodyString ValueForPut:(NSString *)valueforput 
{ 

NSString *strUrl= @"http://xoap.weather.com/weather/local/33135?cc=*&dayf=5&link=xoap&prod=xoap&par=1251259247&key=39128d1062f86c8e"; 
NSURL *url=[NSURL URLWithString:strUrl]; 
strUrl=[strUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 

NSLog(@"domainURL :: %@",url); 
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url]; 

[req setTimeoutInterval:60.0]; 
[req setHTTPMethod:@"GET"]; 
if(theConnection)//NSURLConnection 
    [self Set2Defaults]; 
theConnection=[[NSURLConnection alloc]initWithRequest:req delegate:self]; 
if(theConnection) 
    webData=[[NSMutableData data]retain];//NSMutableData 
else 
    NSLog(@"Connection Failed !!!"); 

NSLog(@"Has got response"); 
} 


#pragma mark - 
#pragma mark NSURL Connection Delegate methods 

-(void) connection:(NSURLConnection *) connection didReceiveResponse:(NSURLResponse *) respons{ 
[webData setLength: 0]; 

} 

-(void) connection:(NSURLConnection *) connection didReceiveData:(NSData *) data { 
[webData appendData:data]; 
} 

-(void) connection:(NSURLConnection *) connection didFailWithError:(NSError *) error { 

NSLog(@"Connection Failed!!!"); 

UIAlertView *noConnect = [[UIAlertView alloc] initWithTitle:@"Error!!!" message:@"Request Failed." delegate:self cancelButtonTitle:@"Retry" otherButtonTitles:@"Cancel",nil]; 
[noConnect show]; 
[noConnect release]; 
} 


-(void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
NSString *theXML = @""; 
theXML = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSASCIIStringEncoding]; 

NSLog(@"theXML Values : %@", theXML); 

ResponseDict = [theXML JSONValue]; 
NSLog(@"Responce is.........##%@",[ResponseDict description]); 

} 

-(void)Set2Defaults { 

if (webData != nil){ 
    [webData release]; 
    webData = nil; 
} 

if (theConnection != nil) { 
    [theConnection release]; 
    if (theConnection != nil) 
     theConnection = nil; 
} 
} 
0
SBJSON *parser2 = [[SBJSON alloc] init]; 

NSString *url_str2=[NSString stringWithFormat:@"http://Example Api Here"]; 

url_str2 = [url_str2 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 

NSURLRequest *request2 =[NSURLRequest requestWithURL:[NSURL URLWithString:url_str2]]; 


NSData *response1 = [NSURLConnection sendSynchronousRequest:request2 returningResponse:nil error:nil]; 

NSString *json_string2 = [[NSString alloc] initWithData:response1 encoding:NSUTF8StringEncoding]; 

NSDictionary *statuses3 = [parser2 objectWithString:json_string2 error:nil]; 

NSArray *news_array=[[statuses3 objectForKey:@"sold_list"] valueForKey:@"list"]; 

for(NSDictionary *news in news_array) 
{ 

    @try { 
     [title_arr addObject:[news valueForKey:@"gtitle"]]; //values Add to title array    

     NSLog(@"Display Title Array"); 

    } 
    @catch (NSException *exception) { 

     [title_arr addObject:[NSString stringWithFormat:@""]]; 
    }