Home > Blockchain >  How can I parse this JSON file in MVC
How can I parse this JSON file in MVC

Time:11-30

"equipmentList": [ { "serial": "12I-284911-246-11", "category": "power", "subcategory": "VIP", "type": "multitrak", "nickname": "ACI", "rails": [ "12I-284911-246-12", "12I-284911-246-13" ], "capacitors": [], "firingConfig": [] }, "mappedFurnaces": [ { "serial": "12I-284911-310-11", "id": 1 } ], "capacitors": [ { "capacitorId": 0, "locationType": "capacitor", "locationRef": 1 }, ]

CodePudding user response:

try this code

using Newtonsoft.Json;

....
var json=" ... your json";
  
 var data = JsonConvert.DeserializeObject<Data>(json); 

    foreach (var furnace in data.EquipmentList[0].MappedFurnaces)
    {
        Console.WriteLine($"Furnace:  serial = {furnace.Serial}  id = {furnace.Id}");

    }
    foreach (var capactor in data.EquipmentList[0].Capacitors)
    {
        Console.WriteLine($"Capacitor:  id = {capactor.CapacitorId}  locationType = {capactor.LocationType}");
    }

classes


    public partial class Data
    {
        [JsonProperty("equipmentList")]
        public List<EquipmentList> EquipmentList { get; set; }
    }

    public partial class EquipmentList
    {
        [JsonProperty("serial")]
        public string Serial { get; set; }

        [JsonProperty("category")]
        public string Category { get; set; }

        [JsonProperty("subcategory")]
        public string Subcategory { get; set; }

        [JsonProperty("type")]
        public string Type { get; set; }

        [JsonProperty("nickname")]
        public string Nickname { get; set; }

        [JsonProperty("rails")]
        public List<string> Rails { get; set; }

        [JsonProperty("capacitors")]
        public List<Capacitor> Capacitors { get; set; }

        [JsonProperty("firingConfig")]
        public List<object> FiringConfig { get; set; }

        [JsonProperty("mappedFurnaces")]
        public List<MappedFurnace> MappedFurnaces { get; set; }
    }

    public partial class Capacitor
    {
        [JsonProperty("capacitorId")]
        public long CapacitorId { get; set; }

        [JsonProperty("locationType")]
        public string LocationType { get; set; }

        [JsonProperty("locationRef")]
        public long LocationRef { get; set; }
    }

    public partial class MappedFurnace
    {
        [JsonProperty("serial")]
        public string Serial { get; set; }

        [JsonProperty("id")]
        public long Id { get; set; }
    }

after fixing your json

{
    "equipmentList": [{
        "serial": "12I-284911-246-11",
        "category": "power",
        "subcategory": "VIP",
        "type": "multitrak",
        "nickname": "ACI",
        "rails": ["12I-284911-246-12", "12I-284911-246-13"],
        "capacitors": [],
        "firingConfig": [],
        "mappedFurnaces": [{
            "serial": "12I-284911-310-11",
            "id": 1
        }],
        "capacitors": [{
            "capacitorId": 0,
            "locationType": "capacitor",
            "locationRef": 1
        }]
    }]
}
  • Related