json解析+ iphone [英] json parsing+iphone

查看:58
本文介绍了json解析+ iphone的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在iphone.i中执行json解析,方法如下:-

how to do json parsing in iphone.i had used below way:-

- (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中所做的精确标记的数据.

please guide me is it true. And how can i able to get the data of exact tag which we do in xml.

推荐答案

听觉是将JSON连接到JSON解析url字符串的简单演示,您必须为此添加JSON框架,但现在在IOS 5中,json已在xcode中内置

Hear is simple demo for json connection to JSON parse url string You Have to add JSON framwork for this but now in IOS 5 json is inbuild in 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;
}
 }

这篇关于json解析+ iphone的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆