Home > Enterprise >  ASP.NET C# Deserialized Json Object
ASP.NET C# Deserialized Json Object

Time:02-15

I have json like this

{

"reader_name":"FX9600EAF871",
"mac_address":"84:24:8D:FC:0E:AD",
"tag_reads":[
    {
        "epc":"E28068100000003C0A05E3B7",
        "antennaPort":"1",
        "peakRssi":"-31",
        "seenCount":"2458",
        "timeStamp":"14/02/2022 22:50:24:356",
        "channelIndex":"5"
    }
]
}

I try using this code

public class TagRead
{
    public string epc { get; set; }
    public string pc { get; set; }
    public string antennaPort { get; set; }
    public string peakRssi { get; set; }
    public string seenCount { get; set; }
    public string timeStamp { get; set; }
    public string phase { get; set; }
    public string channelIndex { get; set; }
    public string isHeartBeat { get; set; }
}

public class Hdr
{
    public string reader_name { get; set; }
    public string mac_address { get; set; }
    public List<TagRead> tag_reads { get; set; }
}


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

When try to print reader name using

deserialized.reader_name

it gets result FX9600EAF87

but when print

deserialized.tag_reads

it get nothing?

my question is How to get epc & antennaport data?

thank you

CodePudding user response:

Because deserialized.tag_reads is a collection instead of base type or string, you might get the result that you didn't want to get.

How to get epc & antennaport data?

you might try to use deserialized.tag_reads with foreach to iterator the collection then do your logic

var deserialized = Newtonsoft.Json.JsonConvert.DeserializeObject<Hdr>(json);
foreach(var item in deserialized.tag_reads){
   //item.epc 
   //item.antennaPort 
}

c# online

  • Related