JSObject到JSArray

时间:2015-12-27 12:10:13

标签: vb.net jsobject

我试图从html文档中读取所有网址。

我使用以下代码:

    Dim html As String =
                                    "var linksArray = new Array(); " &
                                    "for (var i = 0; i < document.links.length; i++) {" &
                                    "linksArray[i] = [String(document.links[i].innerHTML), String(document.links[i].innerText), String(document.links[i].href)];" &
                                    "} " &
                                    "return linksArray;"

    Try
        Dim linksArray As JSObject = _Browser.WebView.EvalScript(String.Format("(function(){{ {0} }})()", html))

        If linksArray Is Nothing Then
            Stop 'this line is not reached, so it should be fine so far
        End If

        'the following line throws an error
        Dim urls As JSArray = linksArray.ToArray()

        For Each link As JSObject In urls
            Dim sInnerHTML As String = link(0).ToString().Trim()
            Dim sInnerText As String = link(1).ToString().Trim()
            Dim sHRef As String = link(2).ToString().Trim()

            If sHRef <> "undefined" Then

                Dim nItem As New clsURL
                nItem.HRef = sHRef
                nItem.InnerHTML = sInnerHTML
                nItem.InnerText = sInnerText

                nList.Add(nItem)
            End If

        Next
    Catch ex As Exception
        Debug.Print(ex.Message.ToString)
        Stop
    End Try

然而,行

 Dim urls As JSArray = linksArray.ToArray()

抛出错误&#34;对象引用未设置为对象实例&#34;。

有人知道如何正确地做到这一点吗?

谢谢!

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:0)

知道了:

        Dim linksArray As JSArray = _Browser.WebView.EvalScript(String.Format("(function(){{ {0} }})()", html))

        For Each obj As Object In linksArray

            Dim sInnerHTML As String = obj(0).ToString().Trim()
            Dim sInnerText As String = obj(1).ToString().Trim()
            Dim sHRef As String = obj(2).ToString().Trim()

        Next