如何在iOS中从url请求中提取数据
我正在编写一个应用程序来扫描条形码,然后查询Outpan.com,以便从条形码号中获取信息。 如何从url请求中提取信息? 下面是成功扫描后的输出示例。 例如,如何将名称存储到NSString中 这是我用来访问信息的代码如何在iOS中从url请求中提取数据,ios,objective-c,barcode,Ios,Objective C,Barcode,我正在编写一个应用程序来扫描条形码,然后查询Outpan.com,以便从条形码号中获取信息。 如何从url请求中提取信息? 下面是成功扫描后的输出示例。 例如,如何将名称存储到NSString中 这是我用来访问信息的代码 - (void) sendRequest { NSString *myString = @"https://api.outpan.com/v2/products/"; NSString *secondhalf= _barcode; NSString *thirdHal
- (void) sendRequest {
NSString *myString = @"https://api.outpan.com/v2/products/";
NSString *secondhalf= _barcode;
NSString *thirdHalf = @"?apikey=935b8b8e102f93220b751a4e1c66126a";
NSString *test = [myString stringByAppendingString:secondhalf];
NSString *text = [test stringByAppendingString:thirdHalf];
if (![text isEqualToString:@""]) {
NSURL *url = [NSURL URLWithString:text];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];
}
}
通常我会尝试使用
NSString* value = [webView stringByEvaluatingJavaScriptFromString:@"document.getElementById('myId').value"];
如果html看起来像这样
<html>
<body>
<input id="myId" type="text" value="TextValue"/>
</body>
</html>
<html><head></head><body><pre style="word-wrap: break-word; white-space: pre- wrap;">{
"gtin": "0014633156720",
"outpan_url": "https:\/\/www.outpan.com\/view_product.php? barcode=0014633156720",
"name": "Battlefield: Bad Company 2",
"attributes": {
"Distribution Media\/Method": "Blu-ray Disc",
"ESRB Rating": "RP (Rating Pending)",
"Manufacturer": "Electronic Arts",
"Manufacturer Part Number": "15672",
"Platform": "PlayStation 3",
"Platform Supported": "PlayStation 3",
"Software Main Type": "Game",
"Software Sub Type": "First Person Shooter"
},
"images": [],
"videos": [],
"categories": []
}</pre></body></html>
但是html是这样的
<html>
<body>
<input id="myId" type="text" value="TextValue"/>
</body>
</html>
<html><head></head><body><pre style="word-wrap: break-word; white-space: pre- wrap;">{
"gtin": "0014633156720",
"outpan_url": "https:\/\/www.outpan.com\/view_product.php? barcode=0014633156720",
"name": "Battlefield: Bad Company 2",
"attributes": {
"Distribution Media\/Method": "Blu-ray Disc",
"ESRB Rating": "RP (Rating Pending)",
"Manufacturer": "Electronic Arts",
"Manufacturer Part Number": "15672",
"Platform": "PlayStation 3",
"Platform Supported": "PlayStation 3",
"Software Main Type": "Game",
"Software Sub Type": "First Person Shooter"
},
"images": [],
"videos": [],
"categories": []
}</pre></body></html>
{
“gtin”:“0014633156720”,
“outpan\u url”:“https:\/\/www.outpan.com\/view\u product.php?条形码=0014633156720”,
“姓名”:“战场:坏连2”,
“属性”:{
“分发介质\方法”:“蓝光光盘”,
“ESRB评级”:“RP(评级待定)”,
“制造商”:“电子艺术”,
“制造商零件号”:“15672”,
“平台”:“PlayStation 3”,
“支持平台”:“PlayStation 3”,
“软件主类型”:“游戏”,
“软件子类型”:“第一人称射击手”
},
“图像”:[],
“视频”:[],
“类别”:[]
}
问题是您试图使用UIWebView发出api请求。api可能会将您的Accepts标头视为文本/html,因此会在网页中向您发送api响应,而不仅仅是原始JSON。尝试使用NSURLSession发出请求
- (void)sendRequest:(id)sender {
NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfig delegate:nil delegateQueue:nil];
NSString *urlString = [NSString stringWithFormat: @"https://api.outpan.com/v2/products/%@", _barcode];
NSURL *url = [NSURL URLWithString: urlString];
NSURLComponents *components = [[NSURLComponents alloc] initWithURL: url resolvingAgainstBaseURL: NO];
NSURLQueryItem *query = [[NSURLQueryItem alloc] initWithName: @"apikey" value: @"935b8b8e102f93220b751a4e1c66126a"];
components.queryItems = @[query];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL: components.URL];
request.HTTPMethod = @"GET";
NSURLSessionDataTask* task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (error == nil) {
NSDictionary *responseBody = [NSJSONSerialization JSONObjectWithData: data options: 0 error: nil];
NSLog(@"got response: %@", responseBody);
} else {
// Failure
NSLog(@"URL Session Task Failed: %@", [error localizedDescription]);
}
}];
[task resume];
}
未测试,因为我没有真正的条形码,但您应该在
responseBody
字典中看到您请求的JSON。问题是您试图使用UIWebView发出api请求。api可能会将您的Accepts标头视为文本/html,因此会在网页中向您发送api响应,而不仅仅是原始JSON。尝试使用NSURLSession发出请求
- (void)sendRequest:(id)sender {
NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfig delegate:nil delegateQueue:nil];
NSString *urlString = [NSString stringWithFormat: @"https://api.outpan.com/v2/products/%@", _barcode];
NSURL *url = [NSURL URLWithString: urlString];
NSURLComponents *components = [[NSURLComponents alloc] initWithURL: url resolvingAgainstBaseURL: NO];
NSURLQueryItem *query = [[NSURLQueryItem alloc] initWithName: @"apikey" value: @"935b8b8e102f93220b751a4e1c66126a"];
components.queryItems = @[query];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL: components.URL];
request.HTTPMethod = @"GET";
NSURLSessionDataTask* task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (error == nil) {
NSDictionary *responseBody = [NSJSONSerialization JSONObjectWithData: data options: 0 error: nil];
NSLog(@"got response: %@", responseBody);
} else {
// Failure
NSLog(@"URL Session Task Failed: %@", [error localizedDescription]);
}
}];
[task resume];
}
没有测试,因为我没有真正的条形码,但是你应该在
responseBody
字典中看到你请求的JSON。代码成功地获得了输出,我现在如何从responseBody提取数据?忽略前面的注释,完全忘了我可以使用NSString*str=[responseBody objectForKey:@“name”];代码成功地获得了输出,我现在如何从responseBody中提取数据?忽略前面的注释,完全忘记我可以使用NSString*str=[responseBody objectForKey:@“name”];