【问题标题】:Assigning Dynamic value within Json在 Json 中分配动态值
【发布时间】:2019-06-28 12:16:02
【问题描述】:

我正在尝试使用"WinHttp.WinHttpRequest.5.1" 来执行POST 请求。如果我按照 #1 所示发送 JSON,它会起作用。但是,我想分配值并使用变量(#2)进行调用。有没有办法在不使用第三方库的情况下做到这一点?

DYNAMIC="Some dynamic value will be here"

'#1 Following works
'body = "{ ""FIRST"": ""dynamic"", ""second"": ""somestring"", ""third"": 100}"

'#2 Folowing call fails.    
body = "{ ""FIRST"": DYNAMIC, ""second"": ""somestring"", ""third"": 100}"

TargetURL = "http://localhost:53518/api/Misc/myMethod/"
Set HTTPReq = CreateObject("WinHttp.WinHttpRequest.5.1")

HTTPReq.SetAutoLogonPolicy 0
HTTPReq.Open "POST", TargetURL, False
HTTPReq.setRequestHeader "Content-Type", "application/json"
HTTPReq.setRequestHeader "User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64)AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36"
HTTPReq.setRequestHeader "Connection", "keep-alive"
HTTPReq.setRequestHeader "CacheControl", "no-cache"
HTTPReq.setRequestHeader "AcceptLanguage", "en-US,en;q=0.9,ht;q=0.8"
HTTPReq.setRequestHeader "AcceptEncoding", "gzip,deflate,br"
HTTPReq.setRequestHeader "Accept", "*/*"
HTTPReq.setRequestHeader "AcceptCharset", "UTF-8"
HTTPReq.send (body)

【问题讨论】:

  • 尝试body = "{ ""FIRST"": " & DYNAMIC & ", ""second"": ""somestring"", ""third"": 100}",其中 DYNAMIC = ""dynamic"" 或其他。
  • @Marco 尼斯。如果我将额外的字符串引号添加到像DYNAMIC="""something""" 这样的动态中,它会起作用。谢谢你。如果您发送这样的解决方案,我会接受您的回答。

标签: json excel vba asp.net-core-webapi winhttprequest


【解决方案1】:

试试:

body = "{ ""FIRST"": " & DYNAMIC & ", ""second"": ""somestring"", ""third"": 100}"

DYNAMIC = """something""".

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-08
    • 1970-01-01
    • 2020-02-13
    • 2014-10-29
    • 1970-01-01
    • 1970-01-01
    • 2016-05-21
    • 2010-12-04
    相关资源
    最近更新 更多