获取 NSString 的一部分
在我现在创建的应用程序中,我使用 goo.gl 的 url 缩短器 api 来缩短 url。我几乎可以正常工作了,我可以发送 longUrl 来检索 NSString 中的短 URL,但它的格式如下:
{
“kind”:“urlshortener#url”,
“id”:“http://goo.gl/something”,
"longUrl": "http://somethinglonggggg/"
}
是否有一种方法可以从中获取 id(短网址)。
这是我到目前为止所拥有的:
NSString *longURL = urlText.text;
NSData *reqData = [[NSString stringWithFormat:@"{\"longUrl\":\"%@\"}", longURL] dataUsingEncoding:NSUTF8StringEncoding];
NSMutableURLRequest *request = [NSMutableURLRequest
requestWithURL:[NSURL URLWithString:@"https://www.googleapis.com/urlshortener/v1/url"]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:20];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:reqData];
NSError *err = [[NSError alloc] init];
NSData *retData = [NSURLConnection sendSynchronousRequest:request
returningResponse:nil
error:&err];
if([err domain]) return;
NSString *retString = [[NSString alloc] initWithData:retData encoding:NSUTF8StringEncoding];
if([retString rangeOfString:@"\"error\""].length) return;
NSLog(@" longUrl equals %@ ", longURL);
NSLog(@" retString equals %@ ", retString);
urlText.text = retString;
In my app i'm creating now i use goo.gl's url shortener api to shorted urls. I have it nearly working, I can send the longUrl to retrieve the short one in a NSString but it's in this format:
{
"kind": "urlshortener#url",
"id": "http://goo.gl/something",
"longUrl": "http://somethinglonggggg/"
}
I just wondered if there is a way to just take the id (short url) from that.
Here's what I have so far:
NSString *longURL = urlText.text;
NSData *reqData = [[NSString stringWithFormat:@"{\"longUrl\":\"%@\"}", longURL] dataUsingEncoding:NSUTF8StringEncoding];
NSMutableURLRequest *request = [NSMutableURLRequest
requestWithURL:[NSURL URLWithString:@"https://www.googleapis.com/urlshortener/v1/url"]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:20];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:reqData];
NSError *err = [[NSError alloc] init];
NSData *retData = [NSURLConnection sendSynchronousRequest:request
returningResponse:nil
error:&err];
if([err domain]) return;
NSString *retString = [[NSString alloc] initWithData:retData encoding:NSUTF8StringEncoding];
if([retString rangeOfString:@"\"error\""].length) return;
NSLog(@" longUrl equals %@ ", longURL);
NSLog(@" retString equals %@ ", retString);
urlText.text = retString;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你确实需要将其转换为 NSDictionary (JSON 语法适用于字典或对象,无论你想如何调用它。Google
API for Objective-C 支持 JSON 解析,将响应转换为对象。
你可以找到它此处
如果您需要的只是解析 JSON,我建议您。要么 JSONkit 或 TouchJSON 的
工作方式非常相似,您给它们字符串,它们会返回对象,
您可以在相应项目的自述文件中找到库的单独使用信息 发现它们是多么容易使用。
。然后你就会 使用以下方式访问不同的值:
这是与 REST 服务交互的最佳方式
希望它有所帮助!
You defenitively need to turn that into an NSDictionary (that JSON syntax is for a dictionary, or an object, however you want to call it.
The google API for Objective-C supports JSON parsing to turn the response into objects.
You can find it here.
If all you need is parsing the JSON, I would recommend either JSONkit or TouchJSON.
They both work in very similar ways, you give them the string and they will give you back objects.
The info for the individual usage of the libraries can be found on the readme of the respective project, there you will find how easy to use they are.
You would then acces the different values using:
This is the best way to interact with REST services.
Hope it helps!