【问题标题】:c# Serialize and jsonc# 序列化和json
【发布时间】:2019-07-28 05:53:24
【问题描述】:

我尝试反序列化:

JsonConvert.DeserializeObject<ContentModel>("{\"Message\":\"asdf\",\"Attachments\":[\"dummy.pdf\",\"unnamed.jpg\"]}"),

内容模型在哪里:

public class ContentModel
{
    public string Message { get; set; }
    public string ContentType { get; set; }
    public string[] Attachments { get; set; }
}

但是由于Attachments中的字符串数组导致反序列化失败。

【问题讨论】:

    标签: c# arrays json


    【解决方案1】:

    您在\"dummy.pdf\"\"unnamed.jpg\" 之间使用的字符并不是真正的逗号!它只是看起来像一个逗号! 删除它并用 , 替换它。

    更新
    正如@dbc 提到的,您的代码中当前使用的字符是FULLWIDTH COMMA

    【讨论】:

      【解决方案2】:

      固定的 JSON 是

       JsonConvert.DeserializeObject<ContentModel>("{\"Message\":\"asdf\",\"Attachments\":[\"dummy.pdf\",\"unnamed.jpg\"]}"),
      

      测试小程序:

      using Newtonsoft.Json;
      using System;
      
      namespace ConsoleApp3
      {
          class Program
          {
              static void Main(string[] args)
              {
                  var y = JsonConvert.DeserializeObject<ContentModel>("{\"Message\":\"asdf\",\"Attachments\":[\"dummy.pdf\",\"unnamed.jpg\"]}");
                  Console.WriteLine(JsonConvert.SerializeObject(y, Formatting.Indented));
      
                  Console.ReadKey();
              }
          }
      
          public class ContentModel
          {
              public string Message { get; set; }
              public string ContentType { get; set; }
              public string[] Attachments { get; set; }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2014-01-06
        • 2016-11-14
        • 2017-05-16
        • 2013-07-07
        • 1970-01-01
        • 1970-01-01
        • 2020-10-23
        • 2012-08-26
        • 1970-01-01
        相关资源
        最近更新 更多