计算循环中值的出现次数

时间:2015-04-21 06:24:02

标签: json vb.net

我正在阅读一个返回Dates的Json数组。

我想要做的是遍历Json数组,存储发生的日期,并计算每个日期发生的次数。

       Using client As New WebClient
            Dim feedResponse As String = client.DownloadString(Url)
            Dim jsonObject As Object = JSONHelper.Deserialize(feedResponse)
            Dim feedArray As JArray = JSONHelper.readJSonArray(jsonObject, "data")

                For i = 0 To feedArray.Count - 1
                    Dim feedDetailsObject As Object = feedArray.ElementAt(i)
                    Dim feeddate = JSONHelper.readJSonElement(feedDetailsObject, "updated_time").ToShortDateString
                Next

            End If
        End Using

我想计算多少次" feeddate"值发生。

在循环中计算相同值的出现的最佳方法是什么?

1 个答案:

答案 0 :(得分:2)

您可以使用LINQ' GroupBy来计算每个updated_time值的出现次数:

Dim groupedDates = feedArray.GroupBy(Function(x) JSONHelper.readJSonElement(x, "updated_time").ToShortDateString)
For Each group In groupedDates
    Dim feedDate = group.Key
    Dim numberOfOccurrence = group.Count()
Next