我如何在 MVC 中解析这个 JSON 文件
How can I parse this JSON file in MVC
“装备清单”:
[
{
“系列”:“12I-284911-246-11”,
“类别”:“电源”,
“子类别”:“贵宾”,
“类型”:“多轨”,
"昵称": "ACI",
“rails”:[“12I-284911-246-12”,“12I-284911-246-13”],
“电容器”:[],
“发射配置”:[]
},
“映射炉”:[
{
“系列”:“12I-284911-310-11”,
“编号”:1
}
],
“电容器”:[
{
“capacitorId”:0,
"locationType": "电容器",
“位置参考”: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
}]
}]
}
“装备清单”: [ { “系列”:“12I-284911-246-11”, “类别”:“电源”, “子类别”:“贵宾”, “类型”:“多轨”, "昵称": "ACI", “rails”:[“12I-284911-246-12”,“12I-284911-246-13”], “电容器”:[], “发射配置”:[] }, “映射炉”:[ { “系列”:“12I-284911-310-11”, “编号”:1 } ], “电容器”:[ { “capacitorId”:0, "locationType": "电容器", “位置参考”: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
}]
}]
}