How to get a specific field from json object by key:value?

781 Views Asked by At

I'm trying to save my data using json. I don't know how to get specific object group from a set of json objects.

[
{
  "dateinformation": "2021-10-05:23:01",
  "id": 1,
  "MoveCount": 3,
  "StartPoint": 2322,
  "TotalDist": 2331,
  "SafeDist": 21332,
  "Feed": 2332,
  "EndPoint":23245,
  "Count":1221,

},
{

  "dateinformation": "2021-10-05:26:01",
  "id": 2,
  "MoveCount": 3,     
  "StartPoint": 2322,
  "TotalDist": 2331,
  "SafeDist": 21332,
  "Feed": 2332,
  "EndPoint":23245,
  "Count":1221,
},
{

  "dateinformation": "2021-10-55:03:01",
  "id": 3,
  "MoveCount": 3,
  "StartPoint": 2322,
  "TotalDist": 2331,
  "SafeDist": 21332,
  "Feed": 2332,
  "EndPoint":23245,
  "Count":1221,

}]

is it possible to get/delete a specific object from a bundle of json object by its "id" value?

1

There are 1 best solutions below

2
On

Do like that

NameSpace will include:

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;

Here you can replace your id by specificId

 List<Root> myDeserializedClass = JsonConvert.DeserializeObject<List<Root>>(myJsonResponse); // myJsonResponse is My Source Json
        int specificId = 123; // my dynamic
        var specficObject = myDeserializedClass.Where(x => x.id == specificId);

Class for Deserialize your JSON response:

     public class Root
        {
            public string dateinformation { get; set; }
            public int id { get; set; }
            public int MoveCount { get; set; }
            public int StartPoint { get; set; }
            public int TotalDist { get; set; }
            public int SafeDist { get; set; }
            public int Feed { get; set; }
            public int EndPoint { get; set; }
            public int Count { get; set; }
        }

Also, I have noticed your JSON last column comma in last that should not be like that "Count" : 1221,