使用NSURL附加字符串

时间:2013-01-29 07:26:15

标签: nsstring nsurl

我有以下字符串,我想跟NSURL追加,并在追加后我希望结果在NSURL

{“deviceid”:“3c27c99ac4b159aca81de8f5d266478f00000000”,“nickname”:“sad”,“gender”:0,“marital”:0,“children”:1,“job”:“asd”,“message”: “性Asd”, “pushid”: “3c27c99ac4b159aca81de8f5d266478f00000000”}

可以,请任何人帮助我。 提前谢谢。

2 个答案:

答案 0 :(得分:0)

NSString *str=@"";

NSString *str1=@"\"deviceid\":\"3c27c99ac4b159aca81de8f5d266478f00000000 \",\"nickname\":\"sad\",\"gender\":0,\"marital\":0,\"children\":1,\"job:\"asd\",\"message\":\"Asd\",\"pushid\":\"3c27c99ac4b159aca81de8f5d266478f00000000\""; 

NSURL *url=[NSURL URLWithString:@"give your url"];

NSArray *components = [url pathComponents]; 

for (NSString *c in components)    
{
    str=[str stringByAppendingString:c];  
}   
str=[str stringByAppendingString:str1];

NSURL *newurl=[NSURL URLWithString:@"str"];    

答案 1 :(得分:0)

您还没有说明您希望最终的URL看起来像什么,所以我假设您要将记录字符串中的名称和值作为查询字符串添加到原始URL。

以下方法在给定基本网址和字符串时会返回合并网址,如上面提供的那样:

-(NSURL *)URLWithRecord:(NSString *)record relativeToURL:(NSURL *)originalURL
{
    NSCharacterSet * unwantedDelimeters = [NSCharacterSet characterSetWithCharactersInString:@"{}"];
    NSCharacterSet * fieldSeperator = [NSCharacterSet characterSetWithCharactersInString:@","];
    NSCharacterSet * nameValueSeperator = [NSCharacterSet characterSetWithCharactersInString:@":"];
    NSCharacterSet * quotes = [NSCharacterSet characterSetWithCharactersInString:@"\""];


    record = [record stringByTrimmingCharactersInSet:unwantedDelimeters];
    NSArray * fields = [record componentsSeparatedByCharactersInSet:fieldSeperator];

    NSMutableString * queryString = [NSMutableString stringWithString:@"?"];

    for (NSUInteger fieldCount = 0; fieldCount < [fields count]; fieldCount++) {

        NSString * field = [fields objectAtIndex:fieldCount];

        NSArray * nameValue = [field componentsSeparatedByCharactersInSet:nameValueSeperator];
        NSString * name = [[nameValue objectAtIndex:0] stringByTrimmingCharactersInSet:quotes];
        NSString * value = [[nameValue objectAtIndex:1] stringByTrimmingCharactersInSet:quotes];

        if (fieldCount == ([fields count]-1) ) {
            [queryString appendFormat:@"%@=%@", name, value];
        } else {
            [queryString appendFormat:@"%@=%@&", name, value];
        }
    }

    NSURL * combinedURL = [NSURL URLWithString:queryString relativeToURL:originalURL];
    return combinedURL;
}

使用以下代码进行测试时:

NSURL * originalURL = [NSURL URLWithString:@"http://www.example.com"];
NSString * string = @"{\"deviceid\":\"3c27c99ac4b159aca81de8f5d266478f00000000\",\"nickname\":\"sad\",\"gender\":0,\"marital\":0,\"children\":1,\"job\":\"asd\",\"message\":\"Asd\",\"pushid\":\"3c27c99ac4b159aca81de8f5d266478f00000000\"}";

NSURL * combinedURL = [self URLWithRecord:string relativeToURL:originalURL];
NSLog(@"result=\"%@\"", [combinedURL absoluteString]);

输出结果为:

result="http://www.example.com?deviceid=3c27c99ac4b159aca81de8f5d266478f00000000&nickname=sad&gender=0&marital=0&children=1&job=asd&message=Asd&pushid=3c27c99ac4b159aca81de8f5d266478f00000000"

提供的方法假设记录字符串中没有错误的空格,并且记录中的名称和值仅包含ASCII数字和字母。如果记录包含包含URL问题字符(例如空格)的名称或值,它将返回nil值。如果您怀疑涉及此类字符,则需要相应地重写该方法 - replacing such characters with URL escape codes