I have following string , which i want to append with NSURL
and after appending i want the result in NSURL
{ "deviceid":"3c27c99ac4b159aca81de8f5d266478f00000000 ","nickname":"sad","gender":0,"marital":0,"children":1,"job":"asd","message":"Asd","pushid":"3c27c99ac4b159aca81de8f5d266478f00000000"}
Can , Anybody help me please . Thanks in advance .
You haven't stated what you expect the final URL to look like, so I have assumed you want to add the names and values from your record string as a query string to the original URL.
The following method will return a combined URL when given a base URL and string like the one you have provided above:
-(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;
}
When tested with the following code:
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]);
The output is:
result="http://www.example.com?deviceid=3c27c99ac4b159aca81de8f5d266478f00000000&nickname=sad&gender=0&marital=0&children=1&job=asd&message=Asd&pushid=3c27c99ac4b159aca81de8f5d266478f00000000"
The method provided assumes that there are no erroneous spaces in the record string and that the names and values in the record only contain ASCII numbers and letters. It will return a nil value if the record contains names or values that contain URL problem characters (such as a space). If you suspect that such characters will be involved, you will need to rewrite the method accordingly - replacing such characters with URL escape codes.