如何在Objective-C中解析HTML响应?

时间:2011-08-02 04:50:11

标签: iphone html objective-c json parsing

如何在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:&regExerror];
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);
}

但是没有任何东西可以存储在数组中...我在几个在线测试人员中测试了正则表达式,其中包含不同的响应部分,并且工作正常......这是我第一次使用正则表达式。我已经查看了类引用,看起来它“应该”工作......

有什么想法吗?谢谢!!! :d

2 个答案:

答案 0 :(得分:4)

如果您没有定位iOS4 +,则可以使用NSScanner。根据页面呈现的可靠程度,您可以使用类似

的内容
NSScanner* scanner = [NSScanner scannerWithString:yourStringHere];
NSString* targetString; //your JSON ends up here
[scanner scanUpToString:@"enrollments: " intoString:NULL];
[scanner scanUpToString:@"\n" intoString:&targetString];

但是,如果您对iOS4 +的定位没有问题,我非常同意j0k NSRegularExpression是可行的方法。如果你不熟悉正则表达式,我建议的模式类似于@"enrollments: \[.*?\],"以匹配整个字符串,或者它是否超级可靠地看起来像那样(即,总是一个 one < / em>具有那些确切的属性的对象,您可以尝试@"enrollments: [{id: '(\d+?)', userId: '(\d+?)'}]"

另一方面,第一个更灵活,您可以轻松使用Nextive JSON之类的东西来解析它。

答案 1 :(得分:2)

如果您正在为iOS4 +开发并且每次都在寻找相同的模式,我会考虑使用NSRegularExpression