-2

我需要从ios中的url读取json数据的代码(目标c)

如果你对我有简单的例子,那么它对我有好处

我想将 read json 方法添加到委托中。

4

3 回答 3

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;
}
于 2012-06-21T12:53:23.267 回答
0

使用ASIHTTPRequest

以下是获取 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..

    }
}

希望这可以帮助..

于 2012-06-21T13:19:44.910 回答
-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;
}
于 2012-06-21T12:56:56.650 回答