将文本字符串转换为HTML字符串

时间:2013-10-11 03:57:21

标签: html json parsing excel-vba vba

有没有办法将下面的文本字符串转换为html字符串而不使用replace?

文字字符串

 {"a":"\u003cul class=\"ylist ylist-bordered search-results\"\u003e\n\t\t\t\n\t\t\t\t\t\u003cli class=\"add-search-result\"\u003e\n\t\t\t\t\t\t\t\u003cdiv class=\"search-result yla biz-listing-large www-yla-unit-color-555555-ffefcb\" data-key=\"ad_business\"\u003e\n\t\t\t\u003cspan class=\"yla-tip\"\u003eYelp Ad\u003c/span\u003e\n\n\n\t\t\u003cdiv class=\"media-block media-block-large clearfix main-attributes\"\u003e\n\t\t\t\u003cdiv"}

HTML字符串

 

{"a": "<ul class=\"ylist ylist-bordered search-results\">\n\t\t\t\n\t\t\t\t\t<li class=\"add-search-result\">\n\t\t\t\t\t\t\t<div class=\"search-result yla biz-listing-large www-yla-unit-color-555555-ffefcb\" data-key=\"ad_business\">\n\t\t\t<span class=\"yla-tip\">Yelp Ad</span>\n\n\n\t\t<div class=\"media-block media-block-large clearfix main-attributes\">\n\t\t\t<div class=\"media-avatar\">\n\t\t\t\t\t\t\n\n\n\t<div class=\"photo-box biz-photo-box pb-90s\">\n\t\t\t<a href=\"/adredir?position=0&amp;redirect_url=http%3A%2F%2Fwww.yelp.com%2Fbiz%2Fez-pawn-corp-new-york-3&"}

我尝试了下面的代码,但它没有按预期工作(excel-vba)

 Dim html As Object
    Set html = CreateObject("htmlfile")
    html.body.innerText = strDiv
    Debug.Print   html.body.innerHTML

如果我将文本字符串复制到http://jsonlint.com/以验证字符串,则返回转换后的html字符串。

1 个答案:

答案 0 :(得分:1)

这对我有用。我没有添加任何错误处理技术。我相信你可以照顾到这一点。

Sub Sample()
    Dim sURL As String: sURL = "http://jsonlint.com/"

    Dim StringtoConvert As String
    StringtoConvert = "{""a"":""\u003cul class=\""ylist ylist-bordered search-results\""\u003e\n\t\t\t\n\t\t\t\t\t\u003cli class=\""add-search-result\""\u003e\n\t\t\t\t\t\t\t\u003cdiv class=\""search-result yla biz-listing-large www-yla-unit-color-555555-ffefcb\"" data-key=\""ad_business\""\u003e\n\t\t\t\u003cspan class=\""yla-tip\""\u003eYelp Ad\u003c/span\u003e\n\n\n\t\t\u003cdiv class=\""media-block media-block-large clearfix main-attributes\""\u003e\n\t\t\t\u003cdiv""}"

    Dim ie As Object

    Set ie = CreateObject("InternetExplorer.Application")
    ie.Visible = True

    ie.navigate sURL

    Do While ie.readystate <> 4: Wait (1): Loop

    ie.document.getElementById("json_input").Value = StringtoConvert
    ie.document.getElementById("validate").Click

    Do While ie.readystate <> 4: Wait (1): Loop

    Debug.Print ie.document.getElementById("json_input").Value
End Sub

Private Sub Wait(ByVal nSec As Long)
    nSec = nSec + Timer
    While nSec > Timer
        DoEvents
    Wend
End Sub

<强>截图:

enter image description here