如何使用C#反序列化json数据

zc0qhyus  于 2023-03-31  发布在  C#
关注(0)|答案(1)|浏览(141)

我需要在c#中反序列化blow JSON数据到类列表。它包含动态数据Egg:MACHINE和MACHINE2,以及SAP数据数组1和2和3是动态数据,如果任何一个引导我到达一个输出,然后要将数据存储到数据库

{"Live Machine Data":{"Date":"31-03-2023","Time":"09:16:41","IP Number":"-","Machine Number":"VMC-03","Machine Name":"VMC003","Machine Status":"0","Program Name":"-","Part Count":"45916","alarmData":[],"Power on Time":"31822844","Running Time":"18366222","Breakdown Time":"1","Idle Time":"13352656"},
"SAP Data":[
   {"1":{"Client ID":"555","Order Number":"11186324","Order Quantity":3,"Material Code":"123","Produced Quantity":0}},
   {"2":{"Client ID":"555","Order Number":"11186325","Order Quantity":4,"Material Code":"123","Produced Quantity":0}},
   {"3":{"Client ID":"555","Order Number":"11186326","Order Quantity":5,"Material Code":"123","Produced Quantity":0}}]},

{"Live Machine Data":{"Date":"31-03-2023","Time":"09:16:36","IP Number":"-","Machine Number":"VMC-03","Machine Name":"VMC003","Machine Status":0,"Program Name":"-","Part Count":1,"alarmData":[],"Power on Time":1,"Running Time":1,"Breakdown Time":1,"Idle Time":1},
"SAP Data":[
   {"1":{"Client ID":"555","Order Number":"11186324","Order Quantity":3,"Material Code":"123","Produced Quantity":0}},
   {"2":{"Client ID":"555","Order Number":"11186325","Order Quantity":4,"Material Code":"123","Produced Quantity":0}},
   {"3":{"Client ID":"555","Order Number":"11186326","Order Quantity":5,"Material Code":"123","Produced Quantity":0}}]
},
{"Live Machine Data":{"Date":"31-03-2023","Time":"09:16:36","IP Number":"-","Machine Number":"VMC-03","Machine Name":"VMC003","Machine Status":0,"Program Name":"-","Part Count":1,"alarmData":[],"Power on Time":1,"Running Time":1,"Breakdown Time":1,"Idle Time":1},
"SAP Data":[
   {"1":{"Client ID":"555","Order Number":"11186324","Order Quantity":3,"Material Code":"123","Produced Quantity":0}},
   {"2":{"Client ID":"555","Order Number":"11186325","Order Quantity":4,"Material Code":"123","Produced Quantity":0}},
   {"3":{"Client ID":"555","Order Number":"11186326","Order Quantity":5,"Material Code":"123","Produced Quantity":0}}]
},
{"Live Machine Data":{"Date":"31-03-2023","Time":"09:16:36","IP Number":"-","Machine Number":"VMC-03","Machine Name":"VMC003","Machine Status":0,"Program Name":"-","Part Count":1,"alarmData":[],"Power on Time":1,"Running Time":1,"Breakdown Time":1,"Idle Time":1},
"SAP Data":[
   {"1":{"Client ID":"555","Order Number":"11186324","Order Quantity":3,"Material Code":"123","Produced Quantity":0}},
   {"2":{"Client ID":"555","Order Number":"11186325","Order Quantity":4,"Material Code":"123","Produced Quantity":0}},
   {"3":{"Client ID":"555","Order Number":"11186326","Order Quantity":5,"Material Code":"123","Produced Quantity":0}}]
},
{"Live Machine Data":{"Date":"31-03-2023","Time":"09:16:36","IP Number":"-","Machine Number":"VMC-03","Machine Name":"VMC003","Machine Status":0,"Program Name":"-","Part Count":1,"alarmData":[],"Power on Time":1,"Running Time":1,"Breakdown Time":1,"Idle Time":1},
"SAP Data":[
   {"1":{"Client ID":"555","Order Number":"11186324","Order Quantity":3,"Material Code":"123","Produced Quantity":0}},
   {"2":{"Client ID":"555","Order Number":"11186325","Order Quantity":4,"Material Code":"123","Produced Quantity":0}},
   {"3":{"Client ID":"555","Order Number":"11186326","Order Quantity":5,"Material Code":"123","Produced Quantity":0}}]
}
]
},
{"MACHINE2":[
{"Live Machine Data":{"Date":"31-03-2023","Time":"09:16:41","IP Number":"-","Machine Number":"VMC-03","Machine Name":"VMC003","Machine Status":"0","Program Name":"-","Part Count":"45916","alarmData":[],"Power on Time":"31822844","Running Time":"18366222","Breakdown Time":"1","Idle Time":"13352656"},
"SAP Data":[
   {"1":{"Client ID":"555","Order Number":"11186324","Order Quantity":3,"Material Code":"123","Produced Quantity":0}},
   {"2":{"Client ID":"555","Order Number":"11186325","Order Quantity":4,"Material Code":"123","Produced Quantity":0}},
   {"3":{"Client ID":"555","Order Number":"11186326","Order Quantity":5,"Material Code":"123","Produced Quantity":0}}]},

{"Live Machine Data":{"Date":"31-03-2023","Time":"09:16:36","IP Number":"-","Machine Number":"VMC-03","Machine Name":"VMC003","Machine Status":0,"Program Name":"-","Part Count":1,"alarmData":[],"Power on Time":1,"Running Time":1,"Breakdown Time":1,"Idle Time":1},
"SAP Data":[
   {"1":{"Client ID":"555","Order Number":"11186324","Order Quantity":3,"Material Code":"123","Produced Quantity":0}},
   {"2":{"Client ID":"555","Order Number":"11186325","Order Quantity":4,"Material Code":"123","Produced Quantity":0}},
   {"3":{"Client ID":"555","Order Number":"11186326","Order Quantity":5,"Material Code":"123","Produced Quantity":0}}]
},
{"Live Machine Data":{"Date":"31-03-2023","Time":"09:16:36","IP Number":"-","Machine Number":"VMC-03","Machine Name":"VMC003","Machine Status":0,"Program Name":"-","Part Count":1,"alarmData":[],"Power on Time":1,"Running Time":1,"Breakdown Time":1,"Idle Time":1},
"SAP Data":[
   {"1":{"Client ID":"555","Order Number":"11186324","Order Quantity":3,"Material Code":"123","Produced Quantity":0}},
   {"2":{"Client ID":"555","Order Number":"11186325","Order Quantity":4,"Material Code":"123","Produced Quantity":0}},
   {"3":{"Client ID":"555","Order Number":"11186326","Order Quantity":5,"Material Code":"123","Produced Quantity":0}}]
},
{"Live Machine Data":{"Date":"31-03-2023","Time":"09:16:36","IP Number":"-","Machine Number":"VMC-03","Machine Name":"VMC003","Machine Status":0,"Program Name":"-","Part Count":1,"alarmData":[],"Power on Time":1,"Running Time":1,"Breakdown Time":1,"Idle Time":1},
"SAP Data":[
   {"1":{"Client ID":"555","Order Number":"11186324","Order Quantity":3,"Material Code":"123","Produced Quantity":0}},
   {"2":{"Client ID":"555","Order Number":"11186325","Order Quantity":4,"Material Code":"123","Produced Quantity":0}},
   {"3":{"Client ID":"555","Order Number":"11186326","Order Quantity":5,"Material Code":"123","Produced Quantity":0}}]
},
{"Live Machine Data":{"Date":"31-03-2023","Time":"09:16:36","IP Number":"-","Machine Number":"VMC-03","Machine Name":"VMC003","Machine Status":0,"Program Name":"-","Part Count":1,"alarmData":[],"Power on Time":1,"Running Time":1,"Breakdown Time":1,"Idle Time":1},
"SAP Data":[
   {"1":{"Client ID":"555","Order Number":"11186324","Order Quantity":3,"Material Code":"123","Produced Quantity":0}},
   {"2":{"Client ID":"555","Order Number":"11186325","Order Quantity":4,"Material Code":"123","Produced Quantity":0}},
   {"3":{"Client ID":"555","Order Number":"11186326","Order Quantity":5,"Material Code":"123","Produced Quantity":0}}]
}]}]

要存储到数据库

ghhkc1vu

ghhkc1vu1#

使用下面的代码:
首先定义相应的模型:

public class Order
{
    [JsonProperty("Client ID")]
    public string ClientID { get; set; }

    [JsonProperty("Order Number")]
    public string OrderNumber { get; set; }

    [JsonProperty("Order Quantity")]
    public int OrderQuantity { get; set; }

    [JsonProperty("Material Code")]
    public string MaterialCode { get; set; }

    [JsonProperty("Produced Quantity")]
    public int ProducedQuantity { get; set; }
}

public class LiveMachineData
{
    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; }
    public List<object> alarmData { 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 SAPDatum
{
    [JsonProperty("1")]
    public Order FirstOrder { get; set; }

    [JsonProperty("2")]
    public Order SecondOrder { get; set; }

    [JsonProperty("3")]
    public Order ThirdOrder { get; set; }
}

public class DataModel
{
    [JsonProperty("Live Machine Data")]
    public LiveMachineData LiveMachineData { get; set; }

    [JsonProperty("SAP Data")]
    public List<SAPDatum> SAPData { get; set; }
}

最后使用以下代码进行反序列化:

var records = JsonConvert.DeserializeObject<IEnumerable<DataModel>>(json);

相关问题