检索JSON数组的索引

时间:2021-06-29 13:20:28

I have a standard JSON array returned to me which I have deserialized with JsonConvert, below being a simplified representative of my situation:

我有一个标准的JSON数组返回给我,我已经用JsonConvert对它进行反序列化,下面是对我的情况的简化表示:

[
  {
    "name": "John",
    "age": "21",
    },
  {
    "name": "Sally",
    "age": "18",
   },
 {
    "name": "Harry",
    "age": "25",
   }
]

...

Public Class myExample
  Public Property name as String
  Public Property age as Integer
End Class

...

Dim serverResponse as string = reader.ReadToEnd()
Dim jsonResult = JsonConvert.DeserializeObject(Of List(Of myExample))(serverResponse)

I can easily retrieve the values of name or age given the item index, e.g.,

根据项目索引,我可以轻松检索姓名或年龄的值,例如,

Dim someValue = jsonResult.Item(1).name ' returns Sally
Dim someOther = jsonResult.Item(1).age  ' returns 18

Yet I wish to return the index (integer) of the array given the name: i.e., how might I search for Sally and retrieve the Item index integer (in this case 1), or in the case of John, return 0? I have not been able to achieve this with jsonResult.IndexOf(), or jsonResult.FindIndex() and searches have not been fruitful.

但是,我希望返回给定名称为:,我如何搜索Sally并检索条目索引整数(在本例中为1),或者在John中返回0?通过jsonResult.IndexOf()或jsonResult.FindIndex(),我无法实现这一点,而且搜索也没有结果。

I have an extensive JSON and wish to loop through names on a dataGridViewer column, returning the ages of all names to another column.

我有一个广泛的JSON,希望循环遍历dataGridViewer列上的名称,并将所有名称的年龄返回到另一个列。

Thanks for helping a novice out!

谢谢你帮助一个新手走出困境!

1 个解决方案

#1


1  

Since deserialization in this manner results in List(of T), the index to the above example can be found through, for example:

由于以这种方式进行反序列化会导致List(of T),因此可以通过以下方法找到上面示例的索引:

jsonResult.FindIndex((Function(s) s.name.Equals(someNameToCheck)))) 

or any other method of finding an item in a list.

或在列表中查找项的任何其他方法。

#1


1  

Since deserialization in this manner results in List(of T), the index to the above example can be found through, for example:

由于以这种方式进行反序列化会导致List(of T),因此可以通过以下方法找到上面示例的索引:

jsonResult.FindIndex((Function(s) s.name.Equals(someNameToCheck)))) 

or any other method of finding an item in a list.

或在列表中查找项的任何其他方法。