【问题标题】:How can I parse this JSON file in MVC [closed]如何在 MVC 中解析这个 JSON 文件 [关闭]
【发布时间】:2022-01-06 10:29:15
【问题描述】:

“设备清单”: [ { “序列”:“12I-284911-246-11”, “类别”:“权力”, "子类别": "VIP", “类型”:“多轨”, "昵称": "ACI", “导轨”:[“12I-284911-246-12”,“12I-284911-246-13”], “电容器”:[], “触发配置”:[] }, “映射炉”:[ { “序列”:“12I-284911-310-11”, “身份证”:1 } ], “电容器”:[ { "capacitorId": 0, “locationType”:“电容器”, “位置参考”:1 }, ]

【问题讨论】:

标签: c# json asp.net-mvc multidimensional-array


【解决方案1】:

试试这个代码

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}");
    }


    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; }
    }

修复你的 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
        }]
    }]
}

【讨论】:

  • 谢谢。这很有帮助。另外,我正在尝试使用 ASP.net MVC 显示值。我设法从 HTML 中显示对象的值,但无法在 mappedFurnaces 和电容器下显示对象。你知道如何解决这个问题吗?非常感谢。
  • @iowa 我用如何使用的示例更新了我的答案。再试一次,让我知道。
  • 非常感谢.... 成功了。非常感谢先生的帮助!!!
  • @iowa 非常欢迎您。请不要忘记接受答案,因为它很有帮助,否则很快就会被否决。要接受答案,只需点击答案旁边的复选标记。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-13
  • 2015-06-09
  • 2012-08-26
  • 1970-01-01
  • 2012-05-01
  • 2021-06-13
相关资源
最近更新 更多