如何从嵌套/交错的Excel对象获取值 – 来自json的数据

我已经尝试了许多JSON加载项的Excel,但我没有运气parsing下面的JSON数据。 最后我设法使用下面的代码

Sub jsonDecode() Dim jsonDecode As Variant jsonText = Worksheets("Sheet3").Range("A1").Value Set sc = CreateObject("ScriptControl"): sc.Language = "JScript" Set jsonDecode = sc.Eval("(" + jsonText + ")") End Sub 

创build一个交错的对象,但不能访问下面的图像中的值。 我已经尝试了以下

 msgbox(jsonDecode.location.id) msgbox(tostring(jsonDecode.location.id)) msgbox(jsonDecode(location(id))) 

任何帮助将非常感激的代码获取标记为A和B在下面的图像中的值:)如果我的术语有点歪曲,请原谅我

干杯!!

Excel本地窗口中数组树的图像

JSON文本是

{“location”:{“id”:2456,“name”:“Tuggerah”,“region”:“中央海岸”,“州”,“NSW”,“邮编”:“2259”,“timeZone”澳大利亚/悉尼 “ ”LAT“: – 33.30701, ”LNG“:151.4159, ”TYPEID“:1}, ”预测“:{ ”天气“:{ ”天“:[{ ”date时间“:” 2016-09- 13 00:00:00“,”条目“:[{”dateTime“:”2016-09-13 00:00:00“,”precisCode“:”阵雨“,”precis“:”晚雨“ “precisOverlayCode”: “”, “夜”:假, “min” 是:10, “最大”:22}]}], “单元”:{ “温度”: “C”}, “issueDateTime”:“2016- 09-13 11:35:20“},”wind“:{”days“:[{”dateTime“:”2016-09-13 00:00:00“,”entries“:[{”dateTime“:” 2016-09-13 00:00:00“,”速度“:9.1,”方向“:287,”directionText“:”WNW“},{”dateTime“:”2016-09-13 01:00:00“ ,“速度”:9.3,“方向”:258,“directionText”:“WSW”},{“dateTime”:“2016-09-13 02:00:00”,“速度”:9.3,“方向” 256,“directionText”:“WSW”},{“dateTime”:“2016-09-13 03:00:00”,“speed”:9.1,“direction”:254,“directionText”:“WSW” {“dateTime”:“2016-09-13 04:00:00”,“speed”:6.9,“direction”:260,“directionText”:“W”},{“dateTime”:“2016-09-13 5点00" 分00秒, “速度”:5.7, “方向”:2 56,“directionText”:“WSW”},{“dateTime”:“2016-09-13 06:00:00”,“speed”:5.7,“direction”:249,“directionText”:“WSW” {“dateTime”:“2016-09-13 07:00:00”,“speed”:5.9,“direction”:245,“directionText”:“WSW”},{“dateTime”:“2016-09-13 08:00:00“,”速度“:5.2,”方向“:254,”directionText“:”WSW“},{”dateTime“:”2016-09-13 09:00:00“,”速度“ “direction”:272,“directionText”:“W”},{“dateTime”:“2016-09-13 10:00:00”,“speed”:4.6,“direction”:281,“directionText” :“W”},{“dateTime”:“2016-09-13 11:00:00”,“speed”:6.1,“direction”:312,“directionText”:“NW”},{“dateTime” “2016-09-13 12:00:00”,“速度”:8,“方向”:14,“directionText”:“NNE”},{“dateTime”:“2016-09-13 13:00:00 “,”方向“:45,”directionText“:”NE“},{”dateTime“:”2016-09-13 14:00:00“,”速度“:9.8,”方向“ :56,“directionText”:“NE”},{“dateTime”:“2016-09-13 15:00:00”,“speed”:9.6,“direction”:77,“directionText”:“ENE”} ,“dateTime”:“2016-09-13 16:00:00”,“speed”:9.4,“direction”:88,“directionText”:“E”},{“dateTime”:“ 13 17:00:00“,”速度“:10.7,”方向 on“:73,”directionText“:”ENE“},{”dateTime“:”2016-09-13 18:00:00“,”speed“:11.9,”direction“:43,”directionText“:”NE “},{”dateTime“:”2016-09-13 19:00:00“,”speed“:12.6,”direction“:28,”directionText“:”NNE“},{”dateTime“ 09-13 20:00:00“,”速度“:11.7,”方向“:11,”directionText“:”N“},{”dateTime“:”2016-09-13 21:00:00“,”方向“:336,”directionText“:”NNW“},{”dateTime“:”2016-09-13 22:00:00“,”速度“:7.8,”方向“:318, “directionText”:“NW”},{“dateTime”:“2016-09-13 23:00:00”,“speed”:4.6,“direction”:304,“directionText”:“NW”}]}] ,“units”:{“speed”:“km / h”},“issueDateTime”:“2016-09-13 12:11:55”}},“forecastGraphs”:{“temperature”:{“dataConfig”: { “系列”:{ “configuration”:{ “ID”: “温度”, “色彩”: “#003355”, “的lineWidth”:2 “行填充”:假 “lineRenderer”: “StraightLineRenderer”,“showPoints “:假” pointFormatter “:” TemperaturePointFormatter “},” yAxisDataMin “:10.7,” yAxisDataMax “:22.2” yAxisMin “:0”,yAxisMax “:32,” 基团 “:[{” date时间 “:1473724800”点 “:[{” × “:1473724800,” Y “:12.4},{” × “:1473728400,” Y“:11。 7},{ “×”:1473732000, “Y”:11.3},{ “×”:1473735600, “Y”:11},{ “×”:1473739200, “Y”:10.9},{ “×”: 1473742800, “Y”:10.7},{ “×”:1473746400, “Y”:11},{ “×”:1473750000, “Y”:12.2},{ “×”:1473753600, “Y”:14.2} ,{ “×”:1473757200, “Y”:16.8},{ “×”:1473760800, “Y”:19.3},{ “×”:1473764400, “Y”:21},{ “×”:1473768000, “Y”:21.9},{ “×”:1473771600, “Y”:22.2},{ “×”:1473775200, “Y”:22.2},{ “×”:1473778800, “Y”:21.7},{ “×”:1473782400, “Y”:20.7},{ “×”:1473786000, “Y”:19.1},{ “×”:1473789600, “Y”:17.6},{ “×”:1473793200,“Y “:16.1},{” × “:1473796800,” Y “:15.3},{” × “:1473800400,” Y “:14.9},{” × “:1473804000,” Y “:14.6},{” × “:1473807600,” Y “:14.1}]}],” 控制点 “:{” 预 “:{” × “:1473721200,” Y “:10.2},” 后 “:{” × “:1473811200,” Y “:13.7}}},”xAxisMin“:1473724800,”xAxisMax“:1473811199},”units“:{”temperature“:”c“},”issueDateTime“:”2016-09-13 07:21:53“ ,“nextIssueDateTime”:“2016-09-13 08:21:53”},“precis”:{“dataConfig”:{“series”:{“config”:{“id”:“precis” :假的, “showPoints”:真实的, “pointRenderer”: “PrecisSummaryPointRenderer”, “pointFormatter”: “PrecisSummaryPointFormatter”},“格罗 UPS “:[{” date时间 “:1473724800,” 点 “:[{” X “:1473728400,” precisCode “:” 部分-多云”, “夜”:真正},{ “X”:1473739200, “precisCode” “雾”, “夜”:真正},{ “X”:1473750000, “precisCode”: “主要是浑浊”, “夜”:假},{ “X”:1473760800, “precisCode”:“mostly-阴天”, “夜”:假},{ “X”:1473771600, “precisCode”: “主要是浑浊”, “夜”:假},{ “X”:1473782400, “precisCode”: “主要是浑浊” “夜”:假},{ “X”:1473793200, “precisCode”: “机会淋浴云”, “夜”:真正},{ “X”:1473804000, “precisCode”: “雨,雨” , “夜间”:真}]}], “控制点”:[]}, “xAxisMin”:1473724800, “xAxisMax”:1473811199}}}, “观察”:{ “观察”:{ “温度”:{”温度 “:18.5,” apparentTemperature “:17.4,” 趋势 “: – 1},” 湿度 “:{” 百分比 “:87},” 露点 “:{” 温度 “:16.3,” 趋势 “:1},”压力 “:{” 压力 “:1019.3,” 趋势 “:空},” 风 “:{” 速度 “:1670,” gustSpeed “:20.4,” 趋势 “:0,” 方向 “:202.5,” directionText“: “SSW”},“rainfall”:{“lastHourAmount”:0,“todayAmount”:0,“since9AMAmount”:0}},“stations”:{“temperature”:{“name”:“Norah H ead AWS“,”距离“:15.5},”压力“:{”name“:”Norah Head AWS“,”distance“:15.5},”wind“:{”name“:”Norah Head AWS“ “:15.5},”降雨量“:{”name“:”Norah Head AWS“,”distance“:15.5}},”issueDateTime“:”2016-09-13 12:20:00“,”units“:{ “温度”: “C”, “量”: “毫米”, “速度”: “公里/小时”, “距离”: “公里”, “压力”: “百帕”}}, “observationalGraphs”:{”压力 “:{” dataConfig “:{” 系列 “:{” configuration “:{” ID “:” 压力 “ ”色彩“: ”#003355“, ”的lineWidth“:2 ”行填充“:假” lineRenderer “:” StraightLineRenderer”, “showPoints”:假 “pointFormatter”: “PressurePointFormatter”}, “yAxisDataMin”:1018.2, “yAxisDataMax”:1020.9, “yAxisMin”:850, “yAxisMax”:1100, “基团”:[ { “date时间”:1473724800, “点”:[{ “×”:1473724800, “Y”:1019.4},{ “×”:1473728400, “Y”:1019.9},{ “×”:1473730200, “Y” :1019.4},{ “×”:1473732000, “Y”:1019.1},{ “×”:1473733800, “Y”:1018.7},{ “×”:1473735600, “Y”:1018.2},{ “x” 的:1473737400, “Y”:1018.5},{ “×”:1473739200, “Y”:1018.8},{ “×”:1473741000, “Y”:1019.1},{ “×”:1473742800, “Y”:1019.1 },{ “×”:1473744600, “Y”:1019.3},{ “×”:1473746400, “Y”:1019.7},{ “×”:1473748200, “Y”:1020},{ “×”:1473750000, “Y”:1020.1},{ “×”:1473751800, “Y”:1020.5},{ “×”:1473753600,“Y “:1020.9},{” × “:1473755400,” Y “:1020.9},{” × “:1473757200,” Y “:1020.9},{” × “:1473759000,” Y “:1020.4},{” × “:1473760800,” Y “:1020.4},{” × “:1473762600,” Y “:1020.5},{” × “:1473764400,” Y “:1020.5},{” × “:1473766200,” Y“: 1019.8},{ “×”:1473768000, “Y”:1019.3}]}], “控制点”:[]}, “xAxisMin”:1473724800, “xAxisMax”:1473897599}, “单元”:{ “压力”: “hpa”}“provider”:{“id”:329,“name”:“Norah Head AWS”,“lat”: – 33.28,“lng”:151.58,“distance”:15.5,“units” “距离”: “公里”}}}, “温度”:{ “dataConfig”:{ “系列”:{ “configuration”:{ “ID”: “温度”, “色彩”: “#003355”,“的lineWidth “:2”,行填充 “:假” lineRenderer “:” StraightLineRenderer”, “showPoints”:假 “pointFormatter”: “TemperaturePointFormatter”}, “yAxisDataMin”:15.1 “yAxisDataMax”:20.1, “yAxisMin”:0 “yAxisMax”:32, “基团”:[{ “date时间”:1473724800, “点”:[{ “×”:1473724800, “Y”:15.6},{ “×”:1473725400, “Y”:16.1 },{ “×”:1473726000, “Y”:16.1},{ “×”:1473726600, “Y”:16.1},{ “×”:1473727200, “Y”:15。 8},{ “×”:1473727800, “Y”:15.9},{ “×”:1473728400, “Y”:16},{ “×”:1473729000, “Y”:15.9},{ “×”: 1473729600, “Y”:15.9},{ “×”:1473730200, “Y”:15.8},{ “×”:1473730800, “Y”:15.6},{ “×”:1473731400, “Y”:15.4} ,{ “×”:1473732000, “Y”:15.4},{ “×”:1473732600, “Y”:15.4},{ “×”:1473733200, “Y”:15.5},{ “×”:1473733800, “Y”:15.3},{ “×”:1473734400, “Y”:15.3},{ “×”:1473735000, “Y”:15.1},{ “×”:1473735600, “Y”:15.3},{ “×”:1473736200, “Y”:15.3},{ “×”:1473736800, “Y”:15.5},{ “×”:1473737400, “Y”:15.5},{ “×”:1473738000,“Y “:15.5},{” × “:1473738600,” Y “:15.4},{” × “:1473739200,” Y “:15.5},{” × “:1473739800,” Y “:15.6},{” × “:1473740400,” Y “:15.7},{” × “:1473741000,” Y “:15.8},{” × “:1473741600,” Y “:15.9},{” × “:1473742200,” Y“: 16.1},{ “×”:1473742800, “Y”:16.2},{ “×”:1473743400, “Y”:16.4},{ “×”:1473744000, “Y”:16.4},{ “×”: 1473744600, “Y”:16.4},{ “×”:1473745200, “Y”:16.4},{ “×”:1473745800, “Y”:16.3},{ “×”:1473746400, “Y”:16.3} ,{ “×”:1473747000, “Y”:16.4},{ “×”:1473747600, “Y”:16.4},{ “×”:1473748200, “Y”:16.5},{ “×”:1473748800, “Y”:16.6},{ “×”:1473749400, “Y”:16.8},{ “×”:1473750000, “Y”:16.8},{ “×”:147375 0600, “Y”:16.9},{ “×”:1473751200, “Y”:17},{ “×”:1473751800, “Y”:17.1},{ “×”:1473752400, “Y”:17.4} ,{ “×”:1473753000, “Y”:17.4},{ “×”:1473753600, “Y”:17.6},{ “×”:1473754200, “Y”:17.9},{ “×”:1473754800, “Y”:17.9},{ “×”:1473755400, “Y”:17.9},{ “×”:1473756000, “Y”:17.9},{ “×”:1473756600, “Y”:18.2},{ “×”:1473757200, “Y”:18.2},{ “×”:1473757800, “Y”:18.3},{ “×”:1473758400, “Y”:18.2},{ “×”:1473759000,“Y “:18.3},{” × “:1473759600,” Y “:18.4},{” × “:1473760200,” Y “:18.6},{” × “:1473760800,” Y “:18.8},{” × “:1473761400,” Y “:18.7},{” × “:1473762000,” Y “:18.6},{” × “:1473762600,” Y “:18.4},{” × “:1473763200,” Y“: 18.6},{ “×”:1473763800, “Y”:19.2},{ “×”:1473764400, “Y”:20.1},{ “×”:1473765000, “Y”:19.6},{ “×”: 1473765600, “Y”:20.1},{ “×”:1473766200, “Y”:20},{ “×”:1473766800, “Y”:20.1},{ “×”:1473767400, “Y”:19.2} ,{ “×”:1473768000, “Y”:18.8},{ “×”:1473768600, “Y”:18.6},{ “×”:1473769200, “Y”:18.5}]}], “控制点”: []},“xAxisMin”:1473724800,“xAxisMax”:1473897599},“units”:{“temperature”:“c”},“provider”:{“id”:329,“name” ”, “LAT”: – 33.28, “LNG”:151.58, “距离”:15.5, “units”:{“distance”:“km”}}}},“regionPrecis”:{“days”:[{“dateTime”:“2016-09-13 00:00:00”,“entries” {“dateTime”:“2016-09-13 00:00:00”,“precis”:“多云。 今天早晨凌乱的雾。 傍晚下雨的可能性很高(70%)。 在下午晚些时候,东北风速在15至20公里/小时之间,然后在晚上北风。“}]}],”issueDateTime“:”2016-09-13 10:41:16“,”name“:”Central海岸“}}

您的方法使用VBA中的ScriptControl对象使用JScript。 由于ScriptControl对象只是一个32位的ActiveX组件,因此不推荐使用这种方法。 它不适用于64位版本的Office。

如果您接受JScript对象与VBA对象不同,则可以使其工作。 所以你需要一个JScript方法来获取JScript对象。

例:

 Sub jsonDecode() Dim jsonDecode As Variant jsonText = Worksheets("Sheet3").Range("A1").Value Set sc = CreateObject("ScriptControl"): sc.Language = "JScript" sc.AddCode "function getProperty(jsonObj, propertyName) { return jsonObj[propertyName]; } " Set jsonDecode = sc.Eval("(" + jsonText + ")") Set oLocation = sc.Run("getProperty", jsonDecode, "location") MsgBox sc.Run("getProperty", oLocation, "id") Set oForecasts = sc.Run("getProperty", jsonDecode, "forecasts") Set oWeather = sc.Run("getProperty", oForecasts, "weather") Set oDays = sc.Run("getProperty", oWeather, "days") Set oDay0 = sc.Run("getProperty", oDays, "0") MsgBox sc.Run("getProperty", oDay0, "dateTime") End Sub 

这里function getProperty是获取JScript对象的JScript方法。

但是如前所述,您应该寻找更好的方法来parsingJSON与VBA。 有一些,如果你search。

您可以使用免费的Microsoft Excel加载项Power Query *(来自Excel 2010)来浏览和提取JSON文件中的数据。

在超级用户你有一个例子 。

* Power Query在Excel 2016中被称为Get&Transform,是软件的完整部分

excel请求插件可能会有所帮助(免责声明:我是这个开源项目的作者)。

你可以在这里find文档,安装脚本等: http : //excel-requests.readthedocs.io/en/latest/ 。

让我知道你是否需要帮助。