Top
Find a Specific Object in JSON Array - Catatan Si Boy
fade
517
post-template-default,single,single-post,postid-517,single-format-standard,eltd-core-1.1.1,flow child-child-ver-1.0.0,flow-ver-1.3.7,,eltd-smooth-page-transitions,ajax,eltd-grid-1300,eltd-blog-installed,page-template-blog-standard,eltd-header-vertical,eltd-sticky-header-on-scroll-up,eltd-default-mobile-header,eltd-sticky-up-mobile-header,eltd-dropdown-default

Find a Specific Object in JSON Array

Find a Specific Object in JSON Array

I have json file that has

[{"a":"Value","b":"Value"},{"a":"Value1","b":"Value1"}]

I want to find a specific object in the JSON array where the a property has the value Value1. In that case I can use Json.Net’s LINQ-to-JSON API (JObjects, etc.) to find it like this:

Dim json As String = _
    "[{""a"":""Value"",""b"":""Value""},{""a"":""Value1"",""b"":""Value1""}]"

Dim array As JArray = JArray.Parse(json)

Dim obj As JObject = _ 
    array.FirstOrDefault(Function(jo As JObject) jo("a").ToString() = "Value1")

If obj Is Nothing Then
    Console.WriteLine("Not found")
Else
    Console.WriteLine("a: " & obj("a").ToString())
    Console.WriteLine("b: " & obj("b").ToString())
End If

 

 

No Comments

Post a Comment