简体   繁体   English

修改 json 查询

[英]Amending a json query

I need to amend a Json with excel vba我需要用excel vba修改一个Json

i want to be able to change the postcode dynamically with VBA or point this part to a cell in the workbook我希望能够使用 VBA 动态更改邮政编码或将此部分指向工作簿中的单元格

let
    Source = Json.Document(Web.Contents("https://api.propertydata.co.uk/prices?key=HEZEHOR0NC&postcode=SW161AG&bedrooms=4")),
    data = Source[data],
    #"Converted to Table" = Record.ToTable(data)
in
    #"Converted to Table"

This assumes a consistent set of object types within the JSON response and uses XMLHTTP request to get the JSON response.这假定 JSON 响应中有一组一致的对象类型,并使用 XMLHTTP 请求来获取 JSON 响应。 This allows you to use a URL query string that concantenates in the postcode.这允许您使用在邮政编码中连接的 URL 查询字符串。 Tested with a couple of postcodes.测试了几个邮政编码。 It uses a JSON parser to handle the JSON.它使用JSON 解析器来处理 JSON。 After importing the JSONConverter.bas you need to go VBE > Tools > References and add a reference to Microsoft Scripting Runtime.导入JSONConverter.bas您需要转到 VBE > Tools > References 并添加对 Microsoft Scripting Runtime 的引用。 Unlike you current M code this will list the pc_ranges values and not just return a object.与您当前的 M 代码不同,这将列出pc_ranges值,而不仅仅是返回一个对象。

Note: You need to replace yourKeyGoesHere with your API key.注意:您需要用您的 API 密钥替换yourKeyGoesHere

Option Explicit
Public r As Long
Public Sub GetInfoFromSheet()
    Application.ScreenUpdating = False
    Dim jsonStr As String, json As Object, item As Object, output As String
    Dim URL As String, postCode As String
    postCode = "SO419AA" '"SW161AG"
    URL = "https://api.propertydata.co.uk/prices?key=yourKeyGoesHere&postcode=" & postCode & "&bedrooms=4"
    r = 1

    With CreateObject("MSXML2.XMLHTTP")
        .Open "GET", URL, False
        .setRequestHeader "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"
        .send
        jsonStr = StrConv(.responseBody, vbUnicode)
    End With
    Set json = JsonConverter.ParseJson(jsonStr)
    emptyObject json
    Application.ScreenUpdating = True
End Sub
Public Sub emptyObject(ByVal json As Object)
    Dim key As Variant, item As Variant
    With ThisWorkbook.Worksheets("Sheet1")
        For Each key In json
            Select Case TypeName(json(key))
            Case "String", "Double"
                .Cells(r, 1) = key
                .Cells(r, 2) = json(key)
                r = r + 1
            Case "Dictionary"
                emptyObject json(key)
            Case "Collection"
                For Each item In json(key)
                    Select Case TypeName(item)
                    Case "Double"
                        .Cells(r, 1) = key
                        .Cells(r, 2) = item
                        r = r + 1
                    Case "Dictionary"
                        emptyObject item
                    End Select
                Next
            End Select
        Next
    End With
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM