首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何使用VBScript或批处理文件从JSON文件下载并获取值?

如何使用VBScript或批处理文件从JSON文件下载并获取值?
EN

Stack Overflow用户
提问于 2018-07-20 05:27:18
回答 1查看 5.6K关注 0票数 0

这是由here应答的VBScript代码,用于从计算机获取具有适当值的JSON文件。

Set fso = CreateObject("Scripting.FileSystemObject")

json = fso.OpenTextFile("C:\path\to\combined.json").ReadAll

Set re = New RegExp
re.Pattern = """passed"":(true|false),"
re.IgnoreCase = True

For Each m In re.Execute(json)
  passed = CBool(m.SubMatches(0))
Next

但是我有一个在线的JSON文件,看起来像这样,

["AA-BB-CC-MAKE-SAME.json","SS-ED-SIXSIX-TENSE.json","FF-EE-EE-EE-WW.json","ZS-WE-AS-FOUR-MINE.json","DD-RF-LATERS-LATER.json","FG-ER-DC-ED-FG.json"]

如何下载此JSON文件并使用VBScript或批处理文件获取五个变量的值?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-20 11:02:58

以下是从互联网上下载并解析json的示例:

Dim http,URL
URL = "http://ip-api.com/json/"
Set http = CreateObject("Msxml2.XMLHTTP")
http.open "GET",URL,False
http.send
strJson = http.responseText

Set j = ParseJson(strJson)
Result = "IP =" & j.query & vbCrlf &_
"ISP = "& j.isp & vbCrlf &_
"Country = "& j.country & vbCrlf &_
"TimeZone = "& j.timezone

Wscript.echo Result
'--------------------------------------------------------
Function ParseJson(strJson)
    Set html = CreateObject("htmlfile")
    Set window = html.parentWindow
    window.execScript "var json = " & strJson, "JScript"
    Set ParseJson = window.json
End Function
'--------------------------------------------------------

你可以试试这段代码:

Dim http,URL
URL = "https://privateURL/jsonfile/"
Set http = CreateObject("Msxml2.XMLHTTP")
http.open "GET",URL,False
http.send
strJson = http.responseText
Result = Extract(strJson,"(\x22(.*)\x22)")
Arr = Split(Result,",")
For each Item in Arr
    wscript.echo Item
Next
'******************************************
Function Extract(Data,Pattern)
   Dim oRE,oMatches,Match,Line
   set oRE = New RegExp
   oRE.IgnoreCase = True
   oRE.Global = True
   oRE.Pattern = Pattern
   set oMatches = oRE.Execute(Data)
   If not isEmpty(oMatches) then
       For Each Match in oMatches  
           Line = Line & Trim(Match.Value) & vbCrlf
       Next
       Extract = Line
   End if
End Function
'******************************************
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51431965

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档