-2

iOSのURLからjsonデータを読み取るためのコードが必要です(Objective c)

あなたが私のための簡単な例を持っているなら、それは私にとって良いことです

デリゲートにreadjsonメソッドを追加したいと思います。

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 に答える