作者:似风似戏是梦而已 | 来源:互联网 | 2023-05-18 15:06
URL = "https://xxxx.xxxxxxxxxx.com/api/getPrice.php"
objHTTP.Open "POST", URL, False
objHTTP.setRequestHeader "Content-type", "application/json"
objHTTP.send JSONStringSend
result = objHTTP.responseText
Set Json = JsonConverter.ParseJson(result)
methodcount = Json("shipping").Count
Above is the code to get the response.
以上是获得响应的代码。
Below is the result I got.
以下是我得到的结果。
{
"shipping":[
{
"name":"Speedpost via Singapore Post",
"price":27.6,
"delivery":"15-25 days"
},
{
"name":"Registered Airmail via Swiss Post",
"price":5.89,
"delivery":"10 - 25 Days delivery"
},
{
"name":"Unregistered Airmail via Singapore Post",
"price":2.27,
"delivery":"12 - 45 Days delivery"
},
{
"name":"Registered Airmail via SF ",
"price":4.36,
"delivery":"10 - 15 business Days delivery"
},
{
"name":"HK DHL",
"price":37.09,
"delivery":"4 - 7 Days delivery"
}
]
}
Got a list of shipping methods as a http object by API call. I want to select the cheapest method and write only the price into a cell.
通过API调用获得作为http对象的送货方法列表。我想选择最便宜的方法,只将价格写入单元格。
How to find the minimum value from all of the key/value pairs?
如何从所有键/值对中找到最小值?
2 个解决方案
0
This is not solution base on json api. Just refer to it(If your data is all as above content).
这不是基于json api的解决方案。只需参考它(如果您的数据都是上述内容)。
Sub test()
Dim s As String
s = Range("a1") '<~~ if your json text in range("a1") else enter json instead
's = json.text
JsonToArray Range("b1"), s, "price"
JsonToArray Range("c1"), s, "name"
JsonToArray Range("d1"), s, "delivery"
JsonToArrayMin Range("a4"), s, "price"
End Sub
Sub test2()
Dim s As String
s = Range("a1") '<~~ if your json text in range("a1") else enter json instead
JsonToArrayMin Range("a4"), s, "price"
End Sub
Sub JsonToArray(rng As Range, Json As String, Item As String)
Dim vR() As Variant, vSplit, v
Dim n As Long, i As Long
Item = Item & Chr(34) & ":"
vSplit = Split(Json, Item)
For i = 1 To UBound(vSplit)
v = vSplit(i)
n = n + 1
ReDim Preserve vR(1 To n)
vR(n) = Split(v, ",")(0)
vR(n) = Replace(vR(n), Chr(34), "")
vR(n) = Replace(vR(n), ":", "")
vR(n) = Replace(vR(n), "}", "")
vR(n) = Replace(vR(n), "]", "")
Next i
If n > 0 Then
rng.Resize(n) = WorksheetFunction.Transpose(vR)
End If
End Sub
Sub JsonToArrayMin(rng As Range, Json As String, Item As String)
Dim vR() As Variant, vSplit, v
Dim n As Long, i As Long
Item = Item & Chr(34) & ":"
vSplit = Split(Json, Item)
For i = 1 To UBound(vSplit)
v = vSplit(i)
n = n + 1
ReDim Preserve vR(1 To n)
vR(n) = Split(v, ",")(0)
vR(n) = Replace(vR(n), Chr(34), "")
vR(n) = Replace(vR(n), ":", "")
vR(n) = Replace(vR(n), "}", "")
vR(n) = Replace(vR(n), "]", "")
vR(n) = Val(vR(n))
Next i
If n > 0 Then
rng = WorksheetFunction.Min(vR)
End If
End Sub