简体   繁体   中英

Get value of Nested JSON objects according to certain value (C#)

I was making a test Xamarin Android application which requires me to parse JSON data. Parsing simple JSON data was easy enough, but this JSON data seems a little too complicated to understand.

What I want to do :

Based on the Latitude and Longitude , I want to extract that particular JSON object.

Here's the data : https://pastebin.com/raw/7QDh2uZH

What I have so far :

private async Task JsonFetcher()
{
    using (var client = new HttpClient())
    {
        client.BaseAddress = new Uri("https://pastebin.com");

        var result = await client.GetAsync("/raw/7QDh2uZH");
        string resultContent = await result.Content.ReadAsStringAsync();

        try
        {
            dynamic obj2 = Newtonsoft.Json.Linq.JObject.Parse(resultContent);
        }
        catch (Exception GettingError)
        {
            throw;
        }
    }
}

What I have right now just desirializes the JSON data and I can get value of one record by accessing it by something like : obj2.meta (will give out the value of meta).

What can I do to extract the particular record? Any idea or suggestions would be great.

You'll have to optimize for your particular purpose, but if you are just trying to find the node with latitude and longitude you are looking for, this will do the trick.

for( int x = 0; x < obj2.data.Count; x++)
{
   var y = obj2.data[x].Location.Latitude;
   var z = obj2.data[x].Location.Longitude;
   if (y.Value == desiredLat && z.Value == desiredLong)
   {
       return obj2.data[x];
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM