如何在iphone.i中做json解析。我用了下面的方法:
- (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中所做的准确的标签数据。
发布于 2011-06-20 16:53:47
...and在iPhone上是这样做的:
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"]);
}发布于 2011-09-30 17:35:29
之后
- (void)connectionDidFinishLoading:(NSURLConnection *)connection你会在这里得到responseData。您必须将其转换为NSString格式,然后简单地编写
[responseString JSONValue];如果您的响应是数组或字典,那么您必须在accordingly.Like中捕获它。如果您的响应是字典格式的,则编写
NSDictionary *tempDict = [responseString JSONValue];我希望这能对你有所帮助。
发布于 2012-01-18 20:44:15
听到是json连接到JSON解析url字符串简单演示,您必须为此添加JSON框架,但现在在iOS5中,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;
}
}https://stackoverflow.com/questions/6408413
复制相似问题