Excel VBA读取网址API 类Json数据并写入单元格的方法

技术流评论9,490阅读模式

示例代码:

Sub Readjson()
Dim http
Dim aa as String
  Set http = CreateObject("Microsoft.XMLHTTP")
  http.Open "POST", "http://api.xxxx.com", False
  http.send ""
  If http.Status = 200 Then
    aa = http.responseText
    MsgBox "成功。"
   Else
    MsgBox "调用失败,错误代码:" & http.Status
    Range("A1").value = aa
End If
End sub

XMLHTTP是一个微软内置方法,可以直接获取API数据。文章源自原紫番博客-https://www.yuanzifan.com/53505.html

这里Http是一个定义的变量,是一个对象。文章源自原紫番博客-https://www.yuanzifan.com/53505.html

该对象的 .responseText方法,是直接返回Json字符串的文章源自原紫番博客-https://www.yuanzifan.com/53505.html

将字符串赋值给变量aa,再把aa写进A1单元格。需要进一步处理,则在AA之中用Js处理Json字符串。文章源自原紫番博客-https://www.yuanzifan.com/53505.html

文章源自原紫番博客-https://www.yuanzifan.com/53505.html 文章源自原紫番博客-https://www.yuanzifan.com/53505.html

 

发表评论

匿名网友
:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:
确定

拖动滑块以完成验证