【问题标题】:Unable Deserialize JSON Array to C# object [closed]无法将 JSON 数组反序列化为 C# 对象 [关闭]
【发布时间】:2021-09-28 12:24:24
【问题描述】:

我有一个 HTML 页面和一些进行 AJAX 调用的 Javascript 代码。在我的 API(C#) 的服务器端,我收到一个 JSON 数组,如下所示,它最多可以有 60 个元素。对于每个元素,我都映射了一个类对象。

但是我无法将数组反序列化为任何 C# 对象。我尝试过 Array[]、ArrayList 甚至 MyObject[],但都没有。

有人可以帮助我如何将每个元素反序列化为 MyObject。

我在 Visual Studio 中添加断点并检查时收到一些“非法字符”消息。

{[
      {
        "PosX": 1458,
        "PosY": 198,
        "Rotation": 250,
        "Width": 63,
        "Height": 25,
        "URL": "http://localhost:7071/2.png",
        "Name": "10",
        "RawURL": "/Case16/10-2.png",
        "BelongsTo": "k",
        "AppliedRotation": 96,
        "ImageID": 49
      },
      {
        "PosX": 1418,
        "PosY": 563,
        "Rotation": 118,
        "Width": 28,
        "Height": 68,
        "URL": "http://localhost:707/11.png",
        "Name": "10",
        "RawURL": "/Case16/10-1.png",
        "BelongsTo": "karyotype",
        "AppliedRotation": 174,
        "ImageID": 48
      }
    ]}

【问题讨论】:

  • 您需要一个key 来映射对象开头的array{data:[....]}
  • 我有 data.Session 包含上述内容。
  • "{[]}" 似乎不适用于嵌套这样的对象和数组。
  • 嗯,这就是我在服务器上得到的结果
  • 仅仅因为它不是有效的JSON,并不意味着我们应该关闭这篇文章。让我们通过解释如何处理这个来提供帮助

标签: javascript c# json


【解决方案1】:

JSON 格式不支持给定结构,您可以使用任何其他 Json 验证器工具(如 JSON lint)对其进行验证。

您可以通过删除外部大括号“{}”或为对象数组添加属性(例如“ImageInfo”)来解决此问题。

{

"ImageInfo":[
  {
    "PosX": 1458,
    "PosY": 198,
    "Rotation": 250,
    "Width": 63,
    "Height": 25,
    "URL": "http://localhost:7071/2.png",
    "Name": "10",
    "RawURL": "/Case16/10-2.png",
    "BelongsTo": "k",
    "AppliedRotation": 96,
    "ImageID": 49
  },
  {
    "PosX": 1418,
    "PosY": 563,
    "Rotation": 118,
    "Width": 28,
    "Height": 68,
    "URL": "http://localhost:707/11.png",
    "Name": "10",
    "RawURL": "/Case16/10-1.png",
    "BelongsTo": "karyotype",
    "AppliedRotation": 174,
    "ImageID": 48
  }
 ]
}

【讨论】:

    【解决方案2】:

    如果您可以编码/控制服务器,那么您应该更新该 logi 以产生有效的语法。如果您有自定义 AJAX 处理代码,当它与 JSON 对象的模式不匹配时,故意尝试包装响应以使其看起来像 json,那么您应该更新该逻辑以注入属性内容的名称,Value 听起来像是一个很好的通用名称......其他答案将提供进一步的指导。

    如果您无法影响字符串并且遇到无效的 JSON 响应,那么我们可以简单地在代码中清理响应您尝试反序列化它:

        string json = @"{[
        {
            ""PosX"": 1458,
            ""PosY"": 198,
            ""Rotation"": 250,
            ""Width"": 63,
            ""Height"": 25,
            ""URL"": ""http://localhost:7071/2.png"",
            ""Name"": ""10"",
            ""RawURL"": ""/Case16/10-2.png"",
            ""BelongsTo"": ""k"",
            ""AppliedRotation"": 96,
            ""ImageID"": 49
        },
        {
            ""PosX"": 1418,
            ""PosY"": 563,
            ""Rotation"": 118,
            ""Width"": 28,
            ""Height"": 68,
            ""URL"": ""http://localhost:707/11.png"",
            ""Name"": ""10"",
            ""RawURL"": ""/Case16/10-1.png"",
            ""BelongsTo"": ""karyotype"",
            ""AppliedRotation"": 174,
            ""ImageID"": 48
        }
    ]}";
    
    // we don't control this, but server or AJAX callback is wrapping the JSON array as an object.
    if (json.StartsWith("{[") && json.EndsWith("]}"))
        json = json.TrimStart('{').TrimEnd('}');
    
    // Now you can try to deserialize:
    var list = Newtonsoft.Json.JsonConvert.DeserializeObject<List<ImagePoco>>(json);
    foreach(var image in list)
    {
        Console.WriteLine($"[{image.ImageID}] ({image.PosX},{image.PosY}) {image.Name} '{image.URL}'");
    }
    

    这会产生以下输出:

    [49] (1458,198) 10 'http://localhost:7071/2.png'
    [48] (1418,563) 10 'http://localhost:707/11.png'
    

    我使用这个类验证了上面的代码,你可以跟着这个小提琴:https://dotnetfiddle.net/JK2JAZ

    public class ImagePoco
    {
        public int PosX { get; set; }
        public int PosY { get; set; }
        public int Rotation { get; set; }
        public int Width { get; set; }
        public int Height { get; set; }
        public string URL { get; set; }
        public string Name { get; set; }
        public string RawURL { get; set; }
        public string BelongsTo { get; set; }
        public int AppliedRotation { get; set; }
        public int ImageID { get; set; }
    }
    

    【讨论】:

      【解决方案3】:

      您能否尝试移除 {}

      [
        {
          "PosX": 1458,
          "PosY": 198,
          "Rotation": 250,
          "Width": 63,
          "Height": 25,
          "URL": "http://localhost:7071/2.png",
          "Name": "10",
          "RawURL": "/Case16/10-2.png",
          "BelongsTo": "k",
          "AppliedRotation": 96,
          "ImageID": 49
        },
        {
          "PosX": 1418,
          "PosY": 563,
          "Rotation": 118,
          "Width": 28,
          "Height": 68,
          "URL": "http://localhost:707/11.png",
          "Name": "10",
          "RawURL": "/Case16/10-1.png",
          "BelongsTo": "karyotype",
          "AppliedRotation": 174,
          "ImageID": 48
        }
      ]
      

      【讨论】:

      • 或者添加属性名
      • 是的,所以这是“ {} ”的问题。我能够删除它,然后一切都从那里开始。
      猜你喜欢
      • 2014-07-24
      • 1970-01-01
      • 2022-07-05
      • 1970-01-01
      • 1970-01-01
      • 2018-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多