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
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
2
Answers
This json is a collection so you’ll need to deserialise it to a collection:
Test:
if you need just one value, you don’t need any custom classes at all