Hi,
I need to deserialize the blow json data to class list in c#.
JavaScript
[
{
"M1": [
{
"Machine Data": {
"Date": "20-02-2023",
"Time": "09:16:41",
"IP Number": "-",
"Machine Number": "M1",
"Machine Name": "M1",
"Machine Status": "0",
"Program Name": "-",
"Part Count": "1",
"Power on Time": "1",
"Running Time": "1",
"Breakdown Time": "1",
"Idle Time": "1"
},
"SP Data": [
{
"Orders": {
"Client ID": "1",
"Order Number": "123",
"Order Quantity": "3",
"Material Code": "123",
"Produced Quantity": "1"
}
},
{
"Orders": {
"Client ID": "2",
"Order Number": "1234",
"Order Quantity": "4",
"Material Code": "123",
"Produced Quantity": "2"
}
},
{
"Orders": {
"Client ID": "2",
"Order Number": "1234",
"Order Quantity": "4",
"Material Code": "123",
"Produced Quantity": "2"
}
}
]
}
]
}
]
Rajkiran SwainRank #165 · 14.4K reputationPosted Mar 31, 2023, 3:38 AM EDT
If the "M1" property is dynamic and may have different values in the JSON, you can define the property as a dictionary with string keys and a value of type
Listlike this:public class Root { public Dictionary> Machines { get; set; } } public class Machine { public MachineData MachineData { get; set; } public List SpData { get; set; } } public class MachineData { public string Date { get; set; } public string Time { get; set; } public string IpNumber { get; set; } public string MachineNumber { get; set; } public string MachineName { get; set; } public string MachineStatus { get; set; } public string ProgramName { get; set; } public string PartCount { get; set; } public string PowerOnTime { get; set; } public string RunningTime { get; set; } public string BreakdownTime { get; set; } public string IdleTime { get; set; } } public class SpData { public Orders Orders { get; set; } } public class Orders { public string ClientId { get; set; } public string OrderNumber { get; set; } public string OrderQuantity { get; set; } public string MaterialCode { get; set; } public string ProducedQuantity { get; set; } }Then you can deserialize the JSON string into a
Listobject like this:This will give you a list of
Rootobjects, each with aDictionary> property calledMachines. The key of the dictionary will be the dynamic property name (e.g. "M1"), and the value will be a list ofMachineobjects with the corresponding data.Jignesh KumarRank #91 · 22.9K reputationPosted Mar 31, 2023, 2:02 AM EDT
Hello,
Try below one,
public class M1 { [JsonProperty("Machine Data")] public MachineData MachineData { get; set; } [JsonProperty("SP Data")] public List SPData { get; set; } } public class MachineData { public string Date { get; set; } public string Time { get; set; } [JsonProperty("IP Number")] public string IPNumber { get; set; } [JsonProperty("Machine Number")] public string MachineNumber { get; set; } [JsonProperty("Machine Name")] public string MachineName { get; set; } [JsonProperty("Machine Status")] public string MachineStatus { get; set; } [JsonProperty("Program Name")] public string ProgramName { get; set; } [JsonProperty("Part Count")] public string PartCount { get; set; } [JsonProperty("Power on Time")] public string PoweronTime { get; set; } [JsonProperty("Running Time")] public string RunningTime { get; set; } [JsonProperty("Breakdown Time")] public string BreakdownTime { get; set; } [JsonProperty("Idle Time")] public string IdleTime { get; set; } } public class Orders { [JsonProperty("Client ID")] public string ClientID { get; set; } [JsonProperty("Order Number")] public string OrderNumber { get; set; } [JsonProperty("Order Quantity")] public string OrderQuantity { get; set; } [JsonProperty("Material Code")] public string MaterialCode { get; set; } [JsonProperty("Produced Quantity")] public string ProducedQuantity { get; set; } } public class Root { public List M1 { get; set; } } public class SPDatum { public Orders Orders { get; set; } }While deserialization use below one,
Rajkiran SwainRank #165 · 14.4K reputationPosted Mar 31, 2023, 2:36 AM EDT
string jsonData = "[{\"M1\":[{\"Machine Data\":{\"Date\":\"20-02-2023\",\"Time\":\"09:16:41\",\"IP Number\":\"-\",\"Machine Number\":\"M1\",\"Machine Name\":\"M1\",\"Machine Status\":\"0\",\"Program Name\":\"-\",\"Part Count\":\"1\",\"Power on Time\":\"1\",\"Running Time\":\"1\",\"Breakdown Time\":\"1\",\"Idle Time\":\"1\"},\"SP Data\":[{\"Orders\":{\"Client ID\":\"1\",\"Order Number\":\"123\",\"Order Quantity\":\"3\",\"Material Code\":\"123\",\"Produced Quantity\":\"1\"}},{\"Orders\":{\"Client ID\":\"2\",\"Order Number\":\"1234\",\"Order Quantity\":\"4\",\"Material Code\":\"123\",\"Produced Quantity\":\"2\"}},{\"Orders\":{\"Client ID\":\"2\",\"Order Number\":\"1234\",\"Order Quantity\":\"4\",\"Material Code\":\"123\",\"Produced Quantity\":\"2\"}}]}]}]"; List machines = JsonConvert.DeserializeObject>(jsonData);Manikandan R128 reputationPosted Mar 31, 2023, 3:34 AM EDT
Thanks for the reply, have another problem inthe top of the json M1 is a dynamic data, how will arraive with class Property
Jignesh KumarRank #91 · 22.9K reputationPosted Mar 31, 2023, 5:29 AM EDT
Hello Maniknandan,
If you are gettting response as dynamic then you can try this one,
dynamic deserializedData = JsonConvert.DeserializeObject(dynamicResponse); if (deserializedData != null) { var propertyname = deserializedData.YourpropertyName; }Jaya PrakashRank #1,293 · 1.3K reputationPosted Mar 31, 2023, 6:16 AM EDT
pls modify according to ur req
for encryption
try { string json = inputTextBox.Text; byte[] bytesToEncode = Encoding.UTF8.GetBytes(json); string encodedJson = Convert.ToBase64String(bytesToEncode); outputTextBox.Text = encodedJson; Label2.Visible = true; Label2.Text ="Encrypted Key :"+ "\n "+ outputTextBox.Text; } catch (Exception ex) { Label1.Visible = true; Label1.Text = "Error: " + ex.Message; }for decryption json
try { string encodedJson = outputTextBox.Text; byte[] bytesToDecode = Convert.FromBase64String(encodedJson); string decodedJson = Encoding.UTF8.GetString(bytesToDecode); decryptedLabel.Visible = true; Label2.Visible = false; decryptedLabel.Text ="Your Entered Json Object :" +"\n"+ decodedJson; } catch (Exception ex) { Label1.Visible = true; Label1.Text = "Error: " + ex.Message; }