简体   繁体   English

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

[英]How do I parse an HTML response in Objective-C?

How do I parse an HTML response in Objective-C, to find a JSON object embedded in the HTML. 如何在Objective-C中解析HTML响应,以找到嵌入在HTML中的JSON对象。

here is the response I'm getting... 这是我得到的回应...

<!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>

Now, how in the world do I get a hold of the: 现在,我如何掌握以下内容:

enrollments: [{id: '2366888', userId: '2269113'}]

and make it a json object so I can retrieve the userId? 并使其成为json对象,以便我可以检索userId?

PS: I already have the response stored in a NSString object.... PS:我已经将响应存储在NSString对象中了。

Thanks in advance!!! 提前致谢!!!


So, I tried the following: 因此,我尝试了以下方法:

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);
}

But nothing gets store in the a array... I tested the regex at a few online testers with different portions of the response and it works fine... this is my first time using regex in general. 但是什么也没存储在一个数组中……我在几个具有不同响应部分的在线测试仪上测试了regex,并且效果很好……这是我第一次使用regex。 I already looked in the class reference and it seems like it "SHOULD" work... 我已经在类参考中查找了,似乎“ SHOULD”工作正常...

Any ideas? 有任何想法吗? THANKS!!! 谢谢!!! :D :D

If you're not targeting iOS4+, you can use NSScanner. 如果您的目标不是iOS4 +,则可以使用NSScanner。 Depending on how reliably that page is rendered, you could use something like 根据页面呈现的可靠性,您可以使用类似

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

However, if you're okay with targeting iOS4+, I'd strongly agree with j0k that NSRegularExpression is the way to go. 但是,如果您可以使用iOS4 +,则我非常同意j0k认为NSRegularExpression是可行的方法。 If you're not familiar with regular expressions, the pattern I'd suggest is something like @"enrollments: \\[.*?\\]," to match the whole string, or if it's super reliably looking like that (ie, always an array with one object with those exact properties, you could try @"enrollments: [{id: '(\\d+?)', userId: '(\\d+?)'}]" . 如果您不熟悉正则表达式,我建议的模式类似于@"enrollments: \\[.*?\\],"以匹配整个字符串,或者看起来非常可靠(例如,始终一个具有一个具有这些确切属性的对象的数组,您可以尝试@"enrollments: [{id: '(\\d+?)', userId: '(\\d+?)'}]"

On the other hand, the first one is more flexible and you can easily use something like Nextive JSON to parse it. 另一方面,第一个更加灵活,您可以轻松使用Nextive JSON之类的内容对其进行解析。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM