热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

ExcelVBA如何从json对列表中查找最小值?-ExcelVBAHowtofindaminimumvaluefromalistofjsonpairs?

URLhttps:xxxx.xxxxxxxxxx.comapigetPrice.phpobjHTTP.OpenPOST,URL,FalseobjHTT
   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 个解决方案

#1


0  

Please see the explanation in the comments

请参阅评论中的说明

Set Json = JsonConverter.ParseJson(result)
Set shipping = Json("shipping") 
methodcount = shipping.Count
targetCell.Value=getMinPrice(shipping) 'Display the min Price in the cell you want.

And here's the function to get minimum price

这是获得最低价格的功能

Public Function getMinPrice(shipping as Variant)As Double 'Variant is nothing but a convenient VBA way to specify that the shipping argument could be of any type. 
    getMinPrice=10000000.0        ' Set initial minPrice
                                  'Iterate through each record in shipping list. For each loop rec is assigned the new set of price list record. The loop is run till all records are exhausted.
    For Each rec In shipping      'rec is nothing but a variable like i=0, So you can replace rec here and in the following statements with whatever you like
                                  'You could avoid using 'Val' function here, I put it as a precautionary measure
        If getMinPrice > Val(rec("price")) Then 'Check if new price is less the minimum we already have
            getMinPrice = Val(rec("price"))     'Set the new minimum Price.
        End If
    Next
End Function

#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

推荐阅读
author-avatar
似风似戏是梦而已
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有