在Windows上的Excel VBA中,如何为已解析的JSON变量获取字符串化
在这里回答我自己的问题.
我已经在Excel VBA中使用 JSON做了一些工作,并发布了大量的发现,我将在Q&格式 https://stackoverflow.com/help/self-answer http://blog.stackoverflow.com/2011/07/its-ok-to-ask-and-answer-your-own-questions/ 所以在stackoverflow的其他地方,人们可以看到有关在VBA中解析JSON的问题,但他们似乎错过了一两个技巧. 首先,我重新使用自定义JSON解析库,而是使用ScriptControl的Eval方法作为我所有JSON代码的基础. 这是问题In Excel VBA on Windows,how to mitigate issue of dot syntax traversal of parsed JSON broken by IDE’s capitalisation behaviour?的先验问题.它显示了如何使用VBA.CallByName更健壮 我们如何改进并获得JSON字符串表示? 以下是您在Debug.Print(?)之后在立即窗口中看到的内容以及将鼠标悬停在变量上的屏幕截图. 这是5系列的问题3.这是完整系列 Q1 In Excel VBA on Windows,how to mitigate issue of dot syntax traversal of parsed JSON broken by IDE’s capitalisation behaviour? Q2 In Excel VBA on Windows,how to loop through a JSON array parsed? Q3 In Excel VBA on Windows,how to get stringified JSON respresentation instead of “[object Object]” for parsed JSON variables? Q4 In Windows Excel VBA,how to get JSON keys to pre-empt “Run-time error ‘438’: Object doesn’t support this property or method”? Q5 In Excel VBA on Windows,for parsed JSON variables what is this JScriptTypeInfo anyway? 解决方法
与使用解析的JSON对象相关的其他堆栈溢出问题的答案使用迷你脚本方法,我们可以在此处使用此方法.
首先,我们承认Douglas Crockford是“Javascript:The Good Parts”(http://shop.oreilly.com/product/9780596517748.do)的作者 从而, 'Tools->References-> 'Microsoft Script Control 1.0; {0E59F1D2-1FBE-11D0-8FF2-00A0D10038BC}; C:WindowsSysWOW64msscript.ocx 'Microsoft Xml,v6.0 Option Explicit Private Function GetScriptEngine() As ScriptControl Static soScriptEngine As ScriptControl If soScriptEngine Is Nothing Then Set soScriptEngine = New ScriptControl soScriptEngine.Language = "JScript" soScriptEngine.AddCode GetJavaScriptLibrary("https://raw.githubusercontent.com/douglascrockford/JSON-js/master/json2.js") soScriptEngine.AddCode "function overrideToString(jsonObj) { jsonObj.toString = function() { return JSON.stringify(this); } }" End If Set GetScriptEngine = soScriptEngine End Function Private Function GetJavaScriptLibrary(ByVal sURL As String) As String Dim xHTTPRequest As MSXML2.XMLHTTP60 Set xHTTPRequest = New MSXML2.XMLHTTP60 xHTTPRequest.Open "GET",sURL,False xHTTPRequest.send GetJavaScriptLibrary = xHTTPRequest.responseText End Function Private Function DecodeJsonString(ByVal JsonString As String) As Object Dim oScriptEngine As ScriptControl Set oScriptEngine = GetScriptEngine Set DecodeJsonString = oScriptEngine.Eval("(" + JsonString + ")") Call oScriptEngine.Run("overrideToString",DecodeJsonString) '* this gives JSON rendering instead of "[object Object]" End Function Private Function GetJSONObject(ByVal obj As Object,ByVal sKey As String) As Object Dim objReturn As Object Set objReturn = VBA.CallByName(obj,sKey,VbGet) Call GetScriptEngine.Run("overrideToString",objReturn) '* this gives JSON rendering instead of "[object Object]" Set GetJSONObject = objReturn End Function Private Sub TestJSONParsingWithCallByName2() Dim sJsonString As String sJsonString = "{'key1': 'value1','key2': { 'key3': 'value3' } }" Dim objJSON As Object Set objJSON = DecodeJsonString(sJsonString) Stop Dim objKey2 As Object Set objKey2 = GetJSONObject(objJSON,"key2") Debug.Print objKey2 Stop End Sub 下面是一个新代码的屏幕截图,其中显示了JScriptTypeInfo变量的字符串化 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- windows-services – 我的Windows服务应用安装项目中的错误
- 在Windows中创建一个带有C dll的r包并确保可移植到linux
- xaml – 如何更改模板中数据透视表项目标题的大小
- windows – 如何设置NTP客户端与NTP服务器同步的频繁周期
- xamarin – Microsoft身份验证库(MSAL)永远不会从Android上
- .Net的好图形库是什么?
- windows – 测试Enter-PSSession是否成功
- windows – 鼠标移动后,SetCursor恢复
- .net – 使用Microsoft的应用程序块
- windows-store-apps – 如何将Windows应用商店8.1应用移植到