【问题标题】:Excel data parsing says error expecting '{' or '['Excel 数据解析显示错误,应为“{”或“[”
【发布时间】:2021-12-15 04:06:54
【问题描述】:

这是我的代码,我尝试了很多搜索,但没有找到正确的解决方案,请在那里提供帮助。

Sub JnewCOP22()
Dim req As New MSXML2.XMLHTTP60
Dim URL As String, ws As Worksheet
Dim json As Object, r, r1 As String, i, j As Integer
URL = "https://www.nseindia.com/api/quote-equity?symbol=DRREDDY&section=trade_info"
With req
.Open "GET", URL, False
.send
Set json = JsonConverter.ParseJson(.responseText)
r = json("securityWiseDP")("quantityTraded")
End With
MsgBox r
End Sub

API 数据 {"noBlockDeals":true,"bulkBlockDeals":[{"name":"Session I"},{"name":"Session II"}],"marketDeptOrderBook":{"totalBuyQuantity":0,"totalSellQuantity": 264,"bid":[{"price":0,"quantity":0},{"price":0,"quantity":0},{"price":0,"quantity":0},{ "price":0,"quantity":0},{"price":0,"quantity":0}],"ask":[{"price":4659.2,"quantity":264},{"price ":0,"quantity":0},{"price":0,"quantity":0},{"price":0,"quantity":0},{"price":0,"quantity": 0}],"tradeInfo":{"totalTradedVolume":1945164,"totalTradedValue":92498.58,"totalMarketCap":7713541.51,"ffmc":5659504.6195251,"impactCost":0.02},"valueAtRisk":{"securityVar":10.89 ,"indexVar":0,"varMargin":10.89,"extremeLossMargin":3.5,"adhocMargin":0,"applicableMargin":14.39}},"securityWiseDP":{"quantityTraded":1945164,"deliveryQuantity":417789, "deliveryToTradedQuantity":21.48,"seriesRemarks":null,"secWiseDelPosDate":"29-OCT-2021 EOD"}}

错误消息说错误解析 json Expecting ' {' or '[ '

【问题讨论】:

  • 这是来自代码的实际json,还是您作为示例获得的这个json?检查代码中出现的实际值 .responseText
  • 您可能需要先获取 cookie,请参阅 here
  • 请告诉我在编码中要改变什么。
  • 这是我想要从中获取值的实际 json。 @ChrisStrickland
  • 我的意思是,我从那个 URL 获得了有效的 JSON,但是当我解析它时,它返回一个值。看到这张图片:i.imgur.com/1g8MGeI.png 我刚收到 421170。这就是为什么我认为有一些问题。您不太可能将有效的 JSON 传递给 JsonConverter 并使其响应该错误。所以第一步是 Debug.Print .responseText 看看它实际上说了什么,因为我看不出它怎么可能是有效的 JSON 并且你得到了那个结果。你的代码对我来说很好用。

标签: json excel vba


【解决方案1】:

看来你的请求并没有像你预期的那样返回 JSON,而是一个普通的 HTML 文档。

为了请求 JSON 文档,您(可能)应该将 HTTP 请求标头 Accept 设置为 application/json

【讨论】:

  • 是的,这几乎肯定是正确的。他在问题中包含了有效的 JSON,但我敢打赌这不是 .responseText 的实际值
  • 请告诉我在编码中哪里有变化。
猜你喜欢
  • 1970-01-01
  • 2010-10-30
  • 1970-01-01
  • 2019-01-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多