提取数据

我已经在JSON串以下列组织数据(举个例子,我已经格式化的一行)提取数据

[ 

{

"id": 60237,

"categories": [

{ "name": "name1" },

{ "name": "name2" }]

}

]

我使用以下类:

Public Class Entry 

Public Property Id As Integer

Public Property Categories As Category()

End Class

Public Class Category

Public Property Name As String

End Class

我一直在使用JSON.NET

Dim entries() As Entry = JsonConvert.DeserializeObject(Of Entry())(JSONSTRING) 

想从JSON字符串中提取,具有LIN deserialised Q请求,来自“类别”字段的数据与已定义类别名称的列表。 例如,我想提取每个类别名称为“name1”和“name2”的数据“行”。该列表可以采用以下形式:dim desiredcategories = {“name1”,“Name2”}

注意:类别名称未由我定义,可以更改。但是,即使其他类别更改或添加或删除,Name1和Name2也保持不变。

你能帮我在VB .NET中写入“where”行吗?

Dim requestanswer = From entry In entries 

Where ???

注意:为简化后,我还没有把“其中”线被制作成相同的请求,其他元件,其JSON字符串和其他类等内。

回答:

让您所需的类别名称的列表:

Dim list As New List(Of String) 

list.Add("name1")

list.Add("name2")

然后你可以用下面的代码做滤波基于所需的类别:

Dim entries as new List(of Entry) 

Dim entry1 as new Entry()

entry1.Id = 60237

Dim category1 as new Category()

category1.Name = "name1"

Dim category2 as new Category()

category2.Name = "name2"

Dim c as new List(of Category)

c.Add(category1)

c.Add(category2)

entry1.Categories = c.ToArray()

entries.Add(entry1)

Dim list As New List(Of String)

list.Add("name1")

list.Add("name2")

Dim requestanswer =(From entry In entries Where(From category In entry.Categories Where list.Contains(category.Name)).Count() > 0 select entry).ToList()

for each ra as Entry in requestanswer

Console.WriteLine(ra.id)

next

以上是 提取数据 的全部内容, 来源链接: utcz.com/qa/267257.html

回到顶部