ASP.NET Core反序列化PHP生成的JSON得到空.NET对象如何解决?
ASP.NET 5.0 Core 反序列化PHP生成的JSON结果为空问题解决
原始JSON样例
PHP生成的JSON数据结构如下:
[{ "model": "XXXXX", "category": "Some Category", "description": "Hydrostatic", "id": "xyz123", "diagram": { "type": "Clutch", "name": "Clutch", "image": { "remote_url": "http://example.com/img/1565602310.png", "local_name": "diagram.png", "width": 919, "height": 668 }, "parts": [{ "part": "010", "partNumber": "N22-45X", "partName": "Super clutch", "qty": 1, "ic": "", "weight": 1.848, "coords": { "xFrom": 552, "yFrom": 540, "xTo": 674, "yTo": 607 } }] } }]
原有实现代码
读取JSON代码
在ASP.NET 5.0 Core环境下可正常读取本地JSON文件内容:
string path = Path.Combine(Environment.WebRootPath, "data/data.json"); string strJson = System.IO.File.ReadAllText(path);
初始反序列化代码
以下代码执行后反序列化结果始终为空:
List<ModelData> md = JsonSerializer.Deserialize<List<ModelData>>(strJson); List<ModelData> m_data = new List<ModelData>();
初始实体类定义
namespace ColemanData.Models { public class ModelData { public string Model { get; set; } public string Category { get; set; } public string Description { get; set; } public string BookCD { get; set; } public Diagram Diagram { get; set; } public List<Part> Parts { get; set; } } public class Diagram { public string Type { get; set; } public string Name { get; set; } public DiagramImage Image { get; set; } } public class DiagramImage { public string RemoteUrl { get; set; } public float Width { get; set; } public float Height { get; set; } public string LocalName { get; set; } } public class Part { public string Label { get; set; } public string Number { get; set; } public string Name { get; set; } public int Quantity { get; set; } public string Ic { get; set; } public float Weight { get; set; } public Coordinates Coordinates { get; set; } } public class Coordinates { public float XStart { get; set; } public float YStart { get; set; } public float XEnd { get; set; } public float YEnd { get; set; } } }
问题解决方法
修改两处即可正常反序列化得到预期对象:
- 调整实体类的属性命名,使其与JSON中的键名完全匹配
- 将系统自带的
JsonSerializer.Deserialize替换为Newtonsoft.Json提供的JsonConvert.DeserializeObject方法,调整后代码为var data = JsonConvert.DeserializeObject<List<ModelData>>(strJson);
注意:由于原始JSON最外层为方括号包裹的数组结构,反序列化时必须指定
List<ModelData>作为泛型类型,才能被解析器正确识别。
修改后的实体类定义如下:
public class Image { public string remote_url { get; set; } public string local_name { get; set; } public int width { get; set; } public int height { get; set; } } public class Coords { public int xFrom { get; set; } public int yFrom { get; set; } public int xTo { get; set; } public int yTo { get; set; } } public class Part { public string part { get; set; } public string partNumber { get; set; } public string partName { get; set; } public int qty { get; set; } public string ic { get; set; } public string pin { get; set; } public string remarks { get; set; } public double lbs { get; set; } public Coords coords { get; set; } } public class Diagram { public string type { get; set; } public string name { get; set; } public Image image { get; set; } public List<Part> parts { get; set; } } public class Root { public string model { get; set; } public string category { get; set; } public string description { get; set; } public string id{ get; set; } public Diagram diagram { get; set; } }
内容的提问来源于stack exchange,提问作者Subrata Sarkar
相关产品推荐
相关产品推荐

