【问题标题】:Cannot parse a PHP generated JSON into a .NET object. Always empty无法将 PHP 生成的 JSON 解析为 .NET 对象。总是空的
【发布时间】:2023-03-11 16:50:02
【问题描述】:

我有许多 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
            }
        }]
    }
}]

我正在尝试在ASP.NET 5.0 Core 中构建解析器。

string path = Path.Combine(Environment.WebRootPath, "data/data.json");
string strJson = System.IO.File.ReadAllText(path);

上述核心sn -p 从文件中提取JSON字符串。现在,我正在尝试将 JSON 转换为对象 ModelData

List<ModelData> md = JsonSerializer.Deserialize<List<ModelData>>(strJson);
List<ModelData> m_data = new List<ModelData>();

但它总是空的。

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

更新:
采用@sam-sj 的想法,我做了以下两个对我有用的更改。

  1. 类中的属性,以便它们与 JSON 中的键匹配
  2. List&lt;ModelData&gt; md = JsonSerializer.Deserialize&lt;List&lt;ModelData&gt;&gt;(strJson); 更改为var data = JsonConvert.DeserializeObject&lt;List&lt;ModelData&gt;&gt;(strJson);

现在我得到了想要的对象。 &lt;List&lt;ModelData&gt;&gt; 是必需的,因为 JSON 包含在方括号内,即 [...],这使解析器能够理解它是一个数组。

修改后的类现在看起来像这样:

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

【问题讨论】:

  • 我不认为 JSON 序列化程序可以将 JSON 数据分解为多个列表项,就像您尝试做的那样。我可能会尝试创建一个新类,将 JSON 输入中的所有数据表示为一个列表,然后将整个 JSON 对象反序列化为这个新类的一个。
  • 谢谢!你能帮我提供一个示例代码还是指导我看一个类似的教程?
  • @sam-sjs,感谢您为我指明了正确的方向。我已经用一个可行的解决方案更新了我的帖子!
  • 嗨@SubrataSarkar,很高兴看到你得到正确答案。同时,请将解决方案添加为answer,但不要质疑并接受。谢谢。
  • @YongShun 我添加了解决方案作为答案。希望这有效!感谢您的指导。

标签: asp.net json jsonserializer


【解决方案1】:

根据@sam-sj 的想法,我做了以下两个对我有用的更改。

  1. 类中的属性,以便它们与 JSON 中的键匹配
  2. List&lt;ModelData&gt; md = JsonSerializer.Deserialize&lt;List&lt;ModelData&gt;&gt;(strJson); 更改为var data = JsonConvert.DeserializeObject&lt;List&lt;ModelData&gt;&gt;(strJson);

现在我得到了想要的对象。 &lt;List&lt;ModelData&gt;&gt; 是必需的,因为 JSON 包含在方括号内,即 [...],这使解析器能够理解它是一个数组。

修改后的类现在看起来像这样:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-30
    • 2016-05-07
    • 1970-01-01
    相关资源
    最近更新 更多