GET请求的URL

时间:2013-02-05 07:05:54

标签: ios json get

要获取JSON我正在使用此答案SBJsonWriter Nested NSDictionary

现在我有一个我称之为{"key1":"bla1","key2":{"a":"a1","b":"b1","c":"c1","d":"d1"},"key3":"bla3"}的刺痛theString 我需要将其添加到网址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"}

然后我通过它制作一个网址 NSURL * url1 = [NSURL URLWithString:urlString];

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

我认为NSURL不想阅读“{”或“}”并认为该网址格式错误。

我如何收到提出GET请求的网址?

1 个答案:

答案 0 :(得分: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);