在Excel VBA中parsingJSON数组

我已经尝试在另一个线程中遵循该方法,但是在代码中存在某些部分,我不明白,并且不适用于我。 我可以评论,因为我没有达到50的声誉。 (我包括来自其他线程的回应)

我试图访问JSON响应如下,我想获得详细数组后水平(列A1-A6)的细节,但在另一篇文章中,我不明白什么脚本控制方法。

并没有任何评论线来解释它是什么。 而我试图使用它,代码只是在sc.Eval失败“var obj =(”&json&“)”

此外,行JSON = {让你的JSON在这里}失败,而是我取而代之的是JSON = RESP,其中RESP是从API返回的输出。

非常感谢您的帮助。

Sub Tester() Dim json As String Dim sc As Object Dim o Set sc = CreateObject("scriptcontrol") sc.Language = "JScript" json = {get your json here} sc.Eval "var obj=(" & json & ")" 'evaluate the json response 'add some accessor functions sc.AddCode "function getSentenceCount(){return obj.sentences.length;}" sc.AddCode "function getSentence(i){return obj.sentences[i];}" Debug.Print sc.Run("getSentenceCount") Set o = sc.Run("getSentence", 0) Debug.Print o.trans, o.orig End Sub 

来自API的JSON响应

  {"details":[ { "trade":"Micro", "trade_tenor":"5yr+" }, { "trade":"Odd", "trade_tenor":"10yr+" }, { "trade":"Round", "trade_tenor":"20yr+" } ]} 

只需要一些小的修改:

 Sub Tester() Dim json As String Dim sc As Object Dim o, i, num Set sc = CreateObject("scriptcontrol") sc.Language = "JScript" json = Range("A1").Value '{get your json here} sc.Eval "var obj=(" & json & ")" 'evaluate the json response 'add some accessor functions sc.AddCode "function getTradeCount(){return obj.details.length;}" sc.AddCode "function getTrade(i){return obj.details[i];}" num = sc.Run("getTradeCount") For i = 0 To num - 1 Set o = sc.Run("getTrade", i) Debug.Print o.trade, o.trade_tenor Next i End Sub