问题描述
我试图在另一个线程中遵循该方法,但是代码中有某些部分我不理解,并且对我不起作用.我可以发表评论,因为我还没有达到50个声誉. (我包括了其他线程的响应)
我正在尝试如下访问JSON响应,我想将Array After After详细信息放入水平(A1-A6列)中,但是在另一篇文章中,我不明白脚本控制方法是什么.
,没有任何评论行解释它是什么.我尝试使用它,代码刚刚在sc.eval" var obj =("&json&")"
上失败.另外,Line 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
问题描述
I have tried to follow the method in another thread, however there are certain part in the code that I don't understand and it didn't work for me. And I could comment as I haven't reached 50 reputations. (i included the response from the other thread)
I am trying to access to Json response as below, I would like to get the array after details into excel horizontally(column A1-A6), however in the other post, I don't understand what the script control method.
And there wasn't any comment line to explain what is it. And I tried to use it, the code just failed at sc.Eval "var obj=(" & json & ")"
Also, the line json = {get your json here} failed, instead i replaced that to Json = resp , where resp is the output returned from the API.
Your help is greatly appreciated.
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
JSON response from API
{"details":[ { "trade":"Micro", "trade_tenor":"5yr+" }, { "trade":"Odd", "trade_tenor":"10yr+" }, { "trade":"Round", "trade_tenor":"20yr+" } ]}
推荐答案
Only needs some minor modifications:
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