在Excel VBA中解析JSON数组[英] Parsing JSON array in Excel VBA

本文是小编为大家收集整理的关于在Excel VBA中解析JSON数组的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我试图在另一个线程中遵循该方法,但是代码中有某些部分我不理解,并且对我不起作用.我可以发表评论,因为我还没有达到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

本文地址:https://www.itbaoku.cn/post/1937755.html

问题描述

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