简体   繁体   English

在iOS中使用字符串解析JSON数据

[英]parsing JSON Data with String in ios

hai i want to send this url with scanCode string the values of this {%@}= {123},{USA111},{UK111},{IND111} ect. 我想使用scanCode字符串发送此网址,其值为{%@} = {123},{USA111},{UK111},{IND111}等的值。 how to pass this values to the url 如何将此值传递给url

NSString * urlString=[NSString stringWithFormat:@"http://1-dot-digiphoto-01.appspot.com/v1/service/rest/getAlbums/QR/{%@}",scanCode];

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];

// Create url connection and fire request
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];

if (!conn)
{
    responseData = nil;
    NSLog(@"Connection Error");

}

You can try this function, the method is using POST variables: 您可以尝试使用此功能,该方法正在使用POST变量:

-(NSData *)post:(NSString *)postString url:(NSString*)urlString{

    //Response data object
    NSData *returnData = [[NSData alloc]init];

    //Build the Request
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:urlString]];
    [request setHTTPMethod:@"POST"];
    [request setValue:[NSString stringWithFormat:@"%lu", (unsigned long)[postString length]] forHTTPHeaderField:@"Content-length"];
    [request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];

    //Send the Request
    returnData = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil];

    //Get the Result of Request
    NSString *response = [[NSString alloc] initWithBytes:[returnData bytes] length:[returnData length] encoding:NSUTF8StringEncoding];

    if (response) {
        NSLog(@"Response >>>> %@",response);
    }


    return returnData;
}

And here is how you would use it. 这就是您将如何使用它。

NSString *postString = [NSString stringWithFormat:@"param=%@", 
                                                  @"{123},{USA111},{UK111},{IND111} "];
NSString *urlString = @"http://1-dot-digiphoto-01.appspot.com/v1/service/rest/getAlbums/QR/";    
NSData *returnData =  [self post:postString url:urlString];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM