Excel VBA导入json文件

时间:2018-01-19 05:36:33

标签: excel-vba vba excel

我想导入这个json文件:

{" status":" ok"," results_time":" 0.6989 sec。"," results_count&#34 ;:1,"结果":[{" date":" 2017-12-01"," site_url":" digitment.com.hk"," site_title":" seo company hong kong |搜索引擎优化服务亚洲"," site_description":"帮助您的业务在第一页的帮助下成长,这是香港最有经验的seo& amp;数字营销公司。今天询问!","观众":{"访问":2471," time_on_site_avg":" 00:01:24" ," page_views_avg":2.262725673935643," bounce_rate":54.94},"流量":{"价值":2471,"百分比& #34;:100,"国家":[{"国家":"香港","价值":1850,&#34 ;%":74.87},{"国家":"印度","价值":252,"百分比":10.24} ,{" country":" Taiwan"," value":115," percent":4.66},{" country&# 34;:"瑞典","价值":107,"百分比":4.35},{"国家":"马来西亚& #34;,"价值":80,"百分比":3.24},{"国家":"其他","价值":6523,"百分比":2.64}],"来源":{"直接":{"价值":586 ,"百分比":23.74}," search_organic":{"价值":1024,"百分比":41.44," top_keywords& #34;:[{" key单词":"第一页hk","值":430,"百分比":42.05},{"关键字":& #34;谷歌\ u8cfc \ u7269","价值":205,"百分比":20.08},{"关键字":" seo& #34;,"价值":195,"百分比":19.06},{"关键字":"第一页","价值":71,"百分比":7.01},{"关键字":" \ u9999 \ u6e2fseo \ u516c \ u53f8","价值& #34;:71,"百分比":7.01}]}," search_ad":{"价值":793,"百分比": 32.09," top_keywords":[{" keyword":" facebook \ u5ee3 \ u544a \ u6536 \ u8cbb"," value":279, "百分比":35.29},{"关键字":" seo","价值":279,"百分比" :35.29},{"关键字":" google adwords","价值":46,"百分比":5.88},{&# 34;关键字":" facebook \ u4ed8 \ u9322 \ u5ee3 \ u544a","价值":46,"百分比":5.88},{&# 34;关键字":" facebook \ u5 ee3 \ u544a","价值":46,"百分比":5.88}]},"推介":{"价值" :0,"百分比":0," top_referrals":[]}," referral_ad":{"值":36,&# 34;百分比":1.46," top_referrals":[{"网站":" hk.entertainment.appledaily.com","价值&# 34;:36,"百分比":100}]},"社交":{"价值":29,"百分比":1.18 ," top_socials":[{"网站":" Facebook","价值":29,"百分比": 100}]}," appstore":{"价值":0,"百分比":0," top_apps":[]}, " mail":{" value":0,"%":0}},"估计":{" 2017- 07-01":7634," 2017-08-01":27912," 2017-09-01":52102," 2017-10-01&#34 ;:2449," 2017-11-01":1964," 2017-12-01":2471}}}]}

Dim Item As Dictionary
Dim Item2 As Dictionary
Dim Item3 As Dictionary
Dim jsonObject As Object
Set JsonTS = FSO.OpenTextFile("dict.competitor", ForReading)
jsonText = JsonTS.ReadAll
JsonTS.Close
Set jsonObject = ParseJson(jsonText)

For Each Item In jsonObject("results")
    Sheets(1).Cells(2, 1).Value = Item("date")
    Sheets(1).Cells(2, 2).Value = Item("site_url")
    Sheets(1).Cells(2, 3).Value = Item("site_title")
    Sheets(1).Cells(2, 4).Value = Item("site_description")
    Sheets(1).Cells(5, 2).Value = Item("audience")("visits")
    Sheets(1).Cells(6, 2).Value = Item("audience")("time_on_site_avg")
    Sheets(1).Cells(7, 2).Value = Item("audience")("page_views_avg")
    Sheets(1).Cells(8, 2).Value = Item("audience")("bounce_rate")

    aa = 12
    For Each Item2 In Item("traffic")("countries")
            Sheets(1).Cells(aa, 3).Value = Item2("country")
            Sheets(1).Cells(aa, 4).Value = Item2("value")
            Sheets(1).Cells(aa, 5).Value = Item2("percent")
            aa = aa + 1

    Next

    For Each Item3 In Item("traffic")("sources")
             MsgBox ("OK")
    Next

Next

导入项目时出错("流量")("来源")。

如何正确导入此嵌套格式?感谢。

1 个答案:

答案 0 :(得分:0)

您可以查看此链接豁免https://support.office.com/en-us/article/Split-text-into-different-columns-with-the-Convert-Text-to-Columns-Wizard-30B14928-5550-41F5-97CA-7A3E9C363ED7
它使用名为Delimiter的函数 步骤1)单击数据>文本到列 step2)启动Convert Text to Columns Wizard。点击定界>下一步。
step3)检查Space,清除其余的框,或者检查逗号和空格,如果你的文本被拆分的话(Smith,John,名字之间用逗号和空格)。您可以在数据预览窗口中查看数据预览。

希望有所帮助