【发布时间】:2011-08-03 21:46:55
【问题描述】:
如何在 Objective-C 中解析 HTML 响应,以找到嵌入在 HTML 中的 JSON 对象。
这是我收到的回复...
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1"><title>
<script src="/Scripts/LocalLogin_vv1CC4D69C143F4D6.js" type="text/javascript"></script>
<script type="text/javascript">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<meta http-equiv=X-UA-Compatible content=IE=EmulateIE7 />
<title></title>
<script type="text/javascript" language="javascript">
FRAME_API = new FrameApi({
userId: '2269113',
proxyUserId: '2269113',
isProxy: false,
username: 'inst1',
enrollments: [{id: '2366888', userId: '2269113'}],
viewAda: false,
// Strings
I18N : {
ItemViewerFrame: 'Item Viewer Frame',
ItemEditorFrame: 'Item Editor Frame',
GroupSetupFrame: 'Group Setup Frame',
notLoggedIn: 'You are no longer logged in.\<br /\>Please click {0} now.',
notConnected: 'You have been disconnected.\<br /\>Please connect and click {0}.',
login: 'Login'
}
});
Ext.onReady(function() {
if (typeof (Ext.QuickTips) != 'undefined') {
Ext.QuickTips.init();
}
var parentApi = FRAME_API.findParentApi(window);
if(parentApi != null) {
FRAME_API = parentApi;
}
else {
FRAME_API.init(15);
}
});
</script>
</head>
</body>
</html>
现在,我到底要如何获得:
enrollments: [{id: '2366888', userId: '2269113'}]
并将其设为 json 对象,以便我可以检索 userId?
PS:我已经将响应存储在一个 NSString 对象中......
提前致谢!!!
所以,我尝试了以下方法:
NSString* regexString =@"enrollments: \[.*?\],";
NSRegularExpressionOptions options = NSRegularExpressionCaseInsensitive;
NSError* regExerror = NULL;
NSRegularExpression* regex = [NSRegularExpression regularExpressionWithPattern:regexString options:options error:®Exerror];
if (regExerror) {
NSLog(@"%@", [regExerror description]);
}
//store the response from the server - HTML FORMAT
NSData * data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString* loginResponse = [[[NSString alloc] initWithData:data encoding: NSASCIIStringEncoding] autorelease];
NSArray* results = [regex matchesInString:loginResponse options:0 range:NSMakeRange(0, [loginResponse length])];
for (NSTextCheckingResult* result in results) {
NSString* resultString = [loginResponse substringWithRange:result.range];
NSLog(@"%@",resultString);
}
但是没有任何内容存储在 a 数组中...我在几个在线测试人员处使用不同部分的响应测试了正则表达式,它工作正常...这是我第一次使用正则表达式。我已经查看了类参考,它似乎“应该”工作......
有什么想法吗?谢谢!!! :D
【问题讨论】:
标签: iphone html objective-c json parsing