这是使用JSONConvertor.bas 和整个 JSON 字符串(不仅仅是来自数据)。您只需调整当前确定 JSON 提取起点和终点的 InStr 方法。我正在从一个单元格中阅读。 “数据”是一个集合。第一项是包含您所追求的值的字典。您只需循环字典的键,如下所示。将这些写入一行很容易,例如,通过使用带有 rowCounter 和 columnCounter 变量的 Cells 添加单元格引用来定位。您将在外部循环中增加 rowCounter 以每次写入新行。
VBA:
Public Sub GetInfoFromSheet()
Dim jsonStr As String, json As Object, key As Variant, columnCounter As Long, rowCounter As Long
jsonStr = ThisWorkbook.Worksheets("Sheet1").[a1]
Set json = JsonConverter.ParseJson(jsonStr)("data")(1)
rowCounter = rowCounter + 1
For Each key In json
columnCounter = columnCounter + 1
ThisWorkbook.Worksheets("Sheet2").cells(rowCounter, columnCounter) = key & " : " & json(key)
Next
End Sub
JSON 树视图:
这是正在遍历的路径的视图:
JSON 字符串:
这是我正在使用的 JSON:
{
"valid": "true",
"tradedDate": "28SEP2018",
"eqLink": "/live_market/dynaContent/live_watch/get_quote/GetQuote.jsp?symbol=ADANIPORTS",
"data": [
{
"annualisedVolatility": "37.35",
"bestBuy": "8.22",
"totalSellQuantity": "6,65,000",
"vwap": "338.01",
"clientWisePositionLimits": "7807220",
"optionType": "-",
"highPrice": "342.35",
"dailyVolatility": "1.95",
"bestSell": "9.22",
"marketLot": "2500",
"sellQuantity5": "7,500",
"marketWidePositionLimits": "156144401",
"sellQuantity4": "2,500",
"sellQuantity3": "2,500",
"sellQuantity2": "2,500",
"underlying": "ADANIPORTS",
"sellQuantity1": "5,000",
"pChange": "0.55",
"premiumTurnover": "-",
"totalBuyQuantity": "4,95,000",
"turnoverinRsLakhs": "15,227.35",
"changeinOpenInterest": "3,35,000",
"strikePrice": "-",
"openInterest": "98,67,500",
"buyPrice2": "338.10",
"buyPrice1": "338.25",
"openPrice": "339.80",
"prevClose": "336.55",
"expiryDate": "25OCT2018",
"lowPrice": "333.75",
"buyPrice4": "338.00",
"buyPrice3": "338.05",
"buyPrice5": "337.95",
"numberOfContractsTraded": "1,802",
"instrumentType": "FUTSTK",
"sellPrice1": "338.50",
"sellPrice2": "338.55",
"sellPrice3": "338.70",
"sellPrice4": "338.75",
"sellPrice5": "338.80",
"change": "1.85",
"pchangeinOpenInterest": "3.51",
"ltp": "8.82",
"impliedVolatility": "-",
"underlyingValue": "336.20",
"buyQuantity4": "7,500",
"buyQuantity3": "2,500",
"buyQuantity2": "5,000",
"buyQuantity1": "2,500",
"buyQuantity5": "5,000",
"settlementPrice": "336.55",
"closePrice": "0.00",
"lastPrice": "338.40"
}
],
"companyName": "Adani Ports and Special Economic Zone Limited",
"lastUpdateTime": "28-SEP-2018 11:41:23",
"isinCode": null,
"ocLink": "/marketinfo/sym_map/symbolMapping.jsp?symbol=ADANIPORTS&instrument=-&date=-&segmentLink=17&symbolCount=2"
}
注意:
如果您只想提取数据部分,而不是较长的字符串,请考虑调整您当前的 Instr 方法以检索以下有效的 JSON 字符串:
[{"annualisedVolatility":"37.35","bestBuy":"6.01","totalSellQuantity":"6,30,000","vwap":"337.78","clientWisePositionLimits":"7807220","optionType":"-","highPrice":"342.35","dailyVolatility":"1.95","bestSell":"7.80","marketLot":"2500","sellQuantity5":"2,500","marketWidePositionLimits":"156144401","sellQuantity4":"7,500","sellQuantity3":"2,500","sellQuantity2":"2,500","underlying":"ADANIPORTS","sellQuantity1":"2,500","pChange":"0.65","premiumTurnover":"-","totalBuyQuantity":"6,20,000","turnoverinRsLakhs":"24,370.83","changeinOpenInterest":"5,15,000","strikePrice":"-","openInterest":"1,00,47,500","buyPrice2":"338.30","buyPrice1":"338.35","openPrice":"339.80","prevClose":"336.55","expiryDate":"25OCT2018","lowPrice":"333.75","buyPrice4":"338.10","buyPrice3":"338.20","buyPrice5":"338.05","numberOfContractsTraded":"2,886","instrumentType":"FUTSTK","sellPrice1":"338.80","sellPrice2":"338.90","sellPrice3":"338.95","sellPrice4":"339.00","sellPrice5":"339.05","change":"2.20","pchangeinOpenInterest":"5.40","ltp":"7.60","impliedVolatility":"-","underlyingValue":"336.85","buyQuantity4":"7,500","buyQuantity3":"2,500","buyQuantity2":"5,000","buyQuantity1":"2,500","buyQuantity5":"5,000","settlementPrice":"336.55","closePrice":"0.00","lastPrice":"338.75"}]