我需要从ios中的url读取json数据的代码(目标c)
如果你有一个简单的例子,那对我有好处
我想将read json方法添加到委托中。
答案 0 :(得分:1)
-(NSString*)SendWebURL:(NSString*) posturl SendWebPostData:(NSString*) post1 {
NSString *data;
if([self ConnectToInternet]){
NSData *postData = [post1 dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:[[NSString alloc] initWithFormat:@"%@",posturl]]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSError *error;
NSURLResponse *response;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
data=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Message" message:@"Internet connection is not available" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alert show];
[alert release];
}
return data;
}
答案 1 :(得分:0)
以下是获取JSON的示例代码..
向您的WS发送调用
NSURL *url = [NSURL URLWithString:@"Your URL"];
ASIFormDataRequest *alertRequest = [ASIFormDataRequest requestWithURL:url];
[alertRequest setRequestMethod:@"POST"];
[alertRequest setDelegate:self];
[alertRequest setTimeOutSeconds:60];
[alertRequest setNumberOfTimesToRetryOnTimeout:4];
[alertRequest addRequestHeader:@"Content-Type" value:@"application/json"];
[alertRequest setDidStartSelector:@selector(getRequestStarted:)];
[alertRequest setDidFinishSelector:@selector(getRequestFinished:)];
[alertRequest setDidFailSelector:@selector(getRequestFailed:)];
[alertRequest appendPostData:[jsonString dataUsingEncoding:NSUTF8StringEncoding]];
[alertRequest startAsynchronous];
获取JSON
- (void)getRequestFinished:(ASIHTTPRequest *)request
{
[HUD hide: YES];
NSString* jsonResponseString = [request responseString];
NSLog(@"Response JSON for Authenticate User: %@", jsonResponseString);
NSDictionary *aAlertResponse = [jsonResponseString objectFromJSONString];
if ([[aAlertResponse valueForKey:@"status"] isEqualToString:@"success"])
{
//From NSDictiornary you can get all json as name value pair..
}
}
希望这会有所帮助..
答案 2 :(得分:-1)
-(NSArray*)StringToArray:(NSString*)str seprator:(NSString*)sep{
NSArray *retArray = [str componentsSeparatedByString:[[NSString alloc] initWithFormat:@"%@",sep]];
return retArray;
}
-(NSString*)getValueFromIndex:(NSString*)indexVal withArray:(NSArray *)warray{
NSString *retVal = @"";
for(id list in warray)
{
NSMutableArray *response = [self performSelector:@selector(StringToArray:seprator:) withObject:list withObject:@"\":\""];
NSString *str = [response objectAtIndex:0];
str = [str stringByReplacingOccurrencesOfString:@"\"" withString:@""];
if([str isEqualToString:indexVal])
{
retVal = [response objectAtIndex:1];
retVal = [retVal stringByReplacingOccurrencesOfString:@"\"" withString:@""];
break;
}
}
return retVal;
}
-(NSString*)getValue:(NSString*)val fromArray:(NSString*)arr{
NSString *retArray = @"";
NSMutableArray *mData = [self performSelector:@selector(StringToArray:seprator:) withObject:arr withObject:@"\",\""];
NSArray *m1;
for(int i=0;i<[mData count];i++)
{
NSString *str= [mData objectAtIndex:0];
str=[str stringByReplacingOccurrencesOfString:@"\"\":" withString:@""];
[mData removeObjectAtIndex:0];
[mData insertObject:str atIndex:0];
m1 = [self performSelector:@selector(StringToArray:seprator:) withObject:[mData objectAtIndex:i] withObject:@"\":\""];
NSString *st = [[NSString alloc] initWithFormat:@"%@",[m1 objectAtIndex:0]];
//UTF8
st = [st stringByReplacingOccurrencesOfString:@"\"" withString:@""];
if([st isEqualToString:val])
{
retArray = [[NSString alloc] initWithFormat:@"%@",[m1 objectAtIndex:1]];
break;
}
}
return retArray;
}
-(NSString*)getImageValue:(NSString*)val fromArray:(NSString*)arr{
//NSString *retArray = @"";
NSArray *mData = [self performSelector:@selector(StringToArray:seprator:) withObject:arr withObject:@","];
NSArray *m1;
//for(int i=0;i<[mData count];i++)
//{
m1 = [self performSelector:@selector(StringToArray:seprator:) withObject:[mData lastObject] withObject:@"image"];
NSString *st = [[NSString alloc] initWithFormat:@"%@",[m1 objectAtIndex:1]];
st= [st stringByReplacingOccurrencesOfString:@"\":\"" withString:@""];
NSLog(@" Image string %@",st);
//UTF8
// st = [st stringByReplacingOccurrencesOfString:@"\"" withString:@""];
//if([st isEqualToString:val])
//{
//retArray = [[NSString alloc] initWithFormat:@"%@",[m1 objectAtIndex:1]];
//break;
//}
//}
NSLog(@"st %@",st);
return st;
}