首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >json parsing+iphone

json parsing+iphone
EN

Stack Overflow用户
提问于 2011-06-20 16:10:51
回答 4查看 999关注 0票数 0

如何在iphone.i中做json解析。我用了下面的方法:

代码语言:javascript
复制
- (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中所做的准确的标签数据。

EN

回答 4

Stack Overflow用户

发布于 2011-06-20 16:53:47

...and在iPhone上是这样做的:

代码语言:javascript
复制
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"]); 
}
票数 1
EN

Stack Overflow用户

发布于 2011-09-30 17:35:29

之后

代码语言:javascript
复制
 - (void)connectionDidFinishLoading:(NSURLConnection *)connection

你会在这里得到responseData。您必须将其转换为NSString格式,然后简单地编写

代码语言:javascript
复制
[responseString JSONValue];

如果您的响应是数组或字典,那么您必须在accordingly.Like中捕获它。如果您的响应是字典格式的,则编写

代码语言:javascript
复制
NSDictionary *tempDict = [responseString JSONValue];

我希望这能对你有所帮助。

票数 1
EN

Stack Overflow用户

发布于 2012-01-18 20:44:15

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

代码语言:javascript
复制
- (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;
}
 }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6408413

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档