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
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
I am trying to create object with values and a multidimensional array from a second classes. What I created:
class ProductData
{
private ProductPrices[] prices;
public string ParentName { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string TechnicalDescription { get; set; }
public bool Obsolete { get; set; }
public bool Deleted { get; set; }
public string Innercarton { get; set; }
public string Outercarton { get; set; }
public string Package { get; set; }
public string Barcode { get; set; }
public decimal Weight { get; set; }
public decimal EndUserPrice { get; set; }
public int MOQ { get; set; }
public bool ComingSoon { get; set; }
public bool NewProduct { get; set; }
public string Equivalent { get; set; }
public string ReplacedBy { get; set; }
public ProductPrices[] Prices
{
get
{
return prices;
}
set
{
prices = value;
}
}
public List<ProductFeatures> Feature { get; set; }
public List<string> Specification { get; set; }
public List<string> Image { get; set; }
public string File { get; set; }
public string Icon { get; set; }
}
class ProductFeatures
{
public string Feature;
}
public class ProductPrices
{
public decimal Price;
public int MinQuantity;
public DateTime validuntil;
public string currency;
}
Now I would like to have for the ProductPrices an multidimensional array.
So, at the end I would have something like this:
Dictionary<string, ProductData> productInfo = new Dictionary<string, ProductData>();
//Adding data to productInfo
string productName = productInfo.Name;
string description = productInfo.Description;
decimal wholesalePrice = productInfo.ProductPrices[0].Price;
int minQty = productInfo.ProductPrices[0].MinQuantity;
Somewhere I'm missing something. I've been searching around for hours trying to find it but unfortunally...
Thanks in advance!
I already found it.
Remove the private ProductPrices[] prices; and change the
public ProductPrices[] Prices
{
get
{
return prices;
}
set
{
prices = value;
}
}
to
public ProductPrices[] Prices { get; set; }
After that, you can do:
ProductData prData = new ProductData();
prData.ParentName = "Parent name";
prData.Name = "Some name";
prData.Prices = new ProductPrices[enterArrayCount];
ProductPrices prdPrices = new ProductPrices();
prdPrices.Price = Convert.ToDecimal(yourDecimalValue);
prdPrices.MinQuantity = 8;
prdPrices.Currency = "Eur";
prData.Prices[firstIndex] = prdPrices; //So firstIndex must increase after a value has been set.
I had a similar problem with a dictionary - now I'm trying to populate a viewmodel, to return a JSON object to a GET request.
My viewmodel is:
public class HotelInventoryta
{
public int api_version { get; set; }
public string lang { get; set; }
public List<Hotel_List_ta> hotels { get; set; }
}
public class Hotel_List_ta
{
public int ta_id { get; set; }
public string partner_id { get; set; }
public string name { get; set; }
public string street { get; set; }
public string city { get; set; }
public string postal_code { get; set; }
public string state { get; set; }
public string country { get; set; }
public double latitude { get; set; }
public double longitude { get; set; }
public string desc { get; set; }
public string url { get; set; }
public string email { get; set; }
public string phone { get; set; }
public string fax { get; set; }
}
My DataBase model is:
[Table("tblHotel")]
public class Hotelta
{
[Key()]
[Column("hotel_id")]
public long hotel_id { get; set; }
public string hotel_name { get; set; }
public string hotel_add1 { get; set; }
public string hotel_towncity { get; set; }
public string hotel_pc { get; set; }
public string hotel_country { get; set; }
public string hotel_pass { get; set; }
public string hotel_email { get; set; }
public string hotel_tel { get; set; }
public string hotel_fax { get; set; }
}
My controller code to populate the viewmodel is:
private HoteltaContext dbh = new HoteltaContext();
//
// GET: /ta/hotel_inventory
[HttpGet]
public HotelInventoryta hotel_inventory(int api_version, string lang)
{
{
HotelInventoryta hotelinventory = new HotelInventoryta();
hotelinventory.api_version = api_version;
hotelinventory.lang = lang;
// Get data from database
var h = dbh.Hotelta.Where(x => x.hotel_id != 0).ToList();
// loop through each result, and add it to the hotelinventory.hotels model
foreach (var ht in h)
{
// I get the exception on the next line
hotelinventory.hotels.Add(new Hotel_List_ta
{
ta_id = 0,
partner_id = ht.hotel_id.ToString(),
name = ht.hotel_name,
street = ht.hotel_add1,
city = ht.hotel_towncity,
postal_code = ht.hotel_pc,
country = ht.hotel_country,
url = "http://www.me.com",
email = ht.hotel_email,
phone = ht.hotel_tel,
fax = ht.hotel_fax
});
}
return hotelinventory;
}
}
The error is:
Object reference not set to an instance of an object
Firstly, can you help me resolve the error - and if possible, confirm if the way I am reading from the database and populating the viewmodel, is the best way to do it?
Thank you, Mark
This is because the hotels property is never initialized. You could do this in the constructor of HotelInventoryta:
public class HotelInventoryta
{
public HotelInventoryta()
{
hotels = new List<Hotel_List_ta>();
}
// ...
}
Now you initialzed the property with an empty collection, so you can add items to it, rather than hotels being null which causes your exception.
ive got a problem decoding a json output from api.openkvk.nl. I only want the ROWS converted to objects, Whats the best way to convert the json rows to a List?
a json example is:
[{"RESULT":{"TYPES":["bigint","varchar","int","int","varchar","varchar","varchar","varchar","varchar","varchar","bigint","varchar","double","double","date"],"HEADER":["kvk","bedrijfsnaam","kvks","sub","adres","postcode","plaats","type","status","website","vestiging","rechtsvorm","lat_rad","lon_rad","anbi"],"ROWS":[["170401310000","Kuijpers Installaties Helmond B.V.","17040131",null,"Panovenweg 18","5708HR","Helmond","Hoofdvestiging",null,null,"16667549",null,"0.89838484100000005","0.098222471000000006",null],["170401310001","KUIJPERS INSTALLATIES B.V.","17040131",null,"VELDZIGT 18","3454PW","DE MEERN","Nevenvestiging","Vestiging is uitgeschreven uit het handelsregister",null,"0",null,null,null,null],["170401310002","Kuijpers Installaties Helmond B.V.","17040131",null,"Heieinde 12","5047SX","Tilburg","Nevenvestiging",null,null,"21790299",null,"0.90039840900000001","0.087662925000000003",null]]}}]
object:
public class KvK
{
public Int64? kvk { get; set; }
public string bedrijfsnaam { get; set; }
public int? kvks { get; set; }
public int? sub { get; set; }
public string adres { get; set; }
public string postcode { get; set; }
public string plaats { get; set; }
public string type { get; set; }
public string status { get; set; }
public string website { get; set; }
public int? vestiging { get; set; }
public string rechtsvorm { get; set; }
public decimal? lat_rad { get; set; }
public decimal? lon_rad { get; set; }
public DateTime? anbi { get; set; }
}
the idea was to use JavaScriptSerializer.deserialize<> but i could not get it working..
var list = new JavaScriptSerializer().Deserialize<List<ROOT>>(json);
public class RESULT
{
public List<string> TYPES { get; set; }
public List<string> HEADER { get; set; }
public List<List<string>> ROWS { get; set; }
}
public class ROOT
{
public RESULT RESULT { get; set; }
}
I've been trying to find an answer to this on google and on SO
but everywhere I have found uses anonymously typed result lists
what I am trying to acheive is to take a List<SecondaryStandard>
and create a grouped List of SecondaryStandard
each SecondaryStandard looks like this
public class SecondaryStandard
{
public string Id { get; set; }
public int IdNumeric { get; set; }
public string IdText { get; set; }
public Sample Sample { get; set; }
public string StandardName { get; set; }
public DateTime DateCompleted { get; set; }
public SamplePoint SamplingPoint{ get; set; }
public Instrument Instrument{ get; set; }
public string ContainerId { get; set; }
public double Value { get; set; }
public string ComponentName { get; set; }
public string PointLocation { get; set; }
public string Description { get; set; }
public string Description2 { get; set; }
public string Analysis { get; set; }
public string Units { get; set; }
}
what i want is a List() where the Value Property is an average of results for each ComponentName.
Any ideas on how I could achieve this in a strongly typed way or do I need to suck it up and use anonymous objects to achieve what I'm looking for?
You mean this?
List<SecondaryStandard> list = new List<SecondaryStandard>();
// populate list
List<SecondaryStandard> result = list
.GroupBy(l => l.ComponentName)
.Select(s => new SecondaryStandard() { ComponentName = s.Key, Value = s.Average(x => x.Value) }).ToList();