Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

C# : How to Deserialize json data

How to deserialized this type of json file usig C#

My Json

[{"data":{"CRC":"a459","PC":"3000","TID":"e2806810200000040a0652c8","antenna":3,"channel":922.75,"eventNum":396,"format":"epc","idHex":"e28068100000003c0a0652c8","peakRssi":-36,"phase":0.0,"reads":36},"timestamp":"2022-09-19T09:03:26.445+0700","type":"SIMPLE"}]

My Model

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

public class TagRead{
     public string TID { get; set; }
}

public class Hdr{
      public List<TagRead> data { get; set; }
}

My Controller

public ActionResult RefreshData()
    {
        string filepath = GetAndGenFilePath();
        FileInfo fileInfo = new FileInfo(filepath);
        bool isFileLocked = IsFileLocked(fileInfo);

        if (isFileLocked)
        {
            return PartialView("TableTagView", GetListTag);
        }

        string rawData = System.IO.File.ReadAllText(filepath);
        string[] lines = rawData.Split(';');

        if (lines[0] == "")
        {
            HttpContext.Session["GetListTag"] = new List<Root>();
            HttpContext.Session["LastChar"] = 0;
        }
        if (lines.Length > 0 && lines[0] != "" && LastChar != rawData.Length)
        {
            HttpContext.Session["GetListTag"] = new List<Root>();
            foreach (var line in lines)
            {
                var deserialized = Newtonsoft.Json.JsonConvert.DeserializeObject<Hdr>(line);
                var tag = deserialized.data.Select(s => new Root
                {
                    TID = s.TID
                }).ToList();
                GetListTag.AddRange(tag);

            }
            HttpContext.Session["LastChar"] = rawData.Length;
        }
        return PartialView("TableTagView", GetListTag);
    }

This json is from file, I want to get value then add into list
the values I want to get is TID,antenna,idHex

Thank you

>Solution :

This json is a collection so you’ll need to deserialise it to a collection:

// List<Hdr>, not Hdr
var listOfHrds = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Hdr>>(json);
public class Hdr{
  public TagRead data { get; set; }
}      

Test:

var json = File.ReadAllText("data.json");

var listOfHdrs = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Hdr>>(json);

Console.WriteLine(listOfHdrs[0].data.TID); // Output: e2806810200000040a0652c8

public class Hdr{
  public TagRead? data { get; set; }
} 

public class TagRead{
     public string? TID { get; set; }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading