2013-02-05 155 views
0

要获得JSON我用这个答案SBJsonWriter Nested NSDictionaryURL的GET请求

现在我有一个刺{"key1":"bla1","key2":{"a":"a1","b":"b1","c":"c1","d":"d1"},"key3":"bla3"},我已经叫theString ,我需要将其添加到URL http://mysyte.net:8888/JSON? 和接收像这http://lcwebtest.sytes.net:8888/JSON?{"key1":"bla1","key2":{"a":"a1","b":"b1","c":"c1","d":"d1"},"key3":"bla3"}

这里是我做的: NSString *urlString = [NSString stringWithFormat:@"http://mysyte.net:8888/JSON?%@",theString];

的NSLog给http://mysyte.net:8888/JSON?{"key2":{"d":"d1","b":"b1","c":"c1","a":"a1"},"key1":"bla1","key3":"bla3"}

然后我从它做一个url NSURL * url1 = [NSURL URLWithString:urlString];

NSLog(@"%@",url1);给我{null}

我认为NSURL不希望阅读“{”或“}”,并认为该URL格式不正确。

我怎样才能收到的网址做出GET请求?

回答

0

我假设NSURL不想读取“{”或“}”,并认为该网址格式不正确。

没错,你不允许在URL中放置一些特殊字符。你想逃避这样的JSON字符串:

CFStringRef escaped = CFURLCreateStringByAddingPercentEscapes(
    NULL, 
    (CFStringRef)theString, // (__bridge CFStringRef)theString if you use ARC 
    CFSTR(""), 
    CFSTR("?&=%,:+-"), 
    kCFStringEncodingUTF8 
); 
NSString *urlString = [NSString stringWithFormat:@"http://mysyte.net:8888/JSON?%@", (NSString *)escaped]; 
CFRelease(escaped); 
+0

非常感谢! – AlexanderZ