0

我有以下字符串,我想附加它NSURL,在附加之后我想要结果NSURL

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

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

4

2 回答 2

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"];    
于 2013-01-29T09:15:54.800 回答
0

您还没有说明您期望最终 URL 的样子,所以我假设您希望将记录字符串中的名称和值作为查询字符串添加到原始 URL。

当给定一个基本 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 值。如果您怀疑会涉及此类字符,则需要相应地重写该方法 -将此类字符替换为 URL 转义码

于 2013-01-29T12:29:57.933 回答