【问题标题】:Map JSON object to C# class property array将 JSON 对象映射到 C# 类属性数组
【发布时间】:2020-02-10 08:10:51
【问题描述】:

JSON

{
"SoftHoldIDs": 444,
"AppliedUsages": [
    {
        "SoftHoldID": 444,
        "UsageYearID": 223232,
        "DaysApplied": 0,
        "PointsApplied": 1
    }
],
"Guests": [
    1,
    2
]

} 在上面的 JSON 中,SoftholdIDs 是整数,AppliedUsages 是 C# 模型中的类数组属性

问题是——我们如何将 JSON 映射到类属性。 类代码

  public class ReservationDraftRequestDto
{

    public int SoftHoldIDs { get; set; }
    public int[] Guests { get; set; }
    public AppliedUsage[] AppliedUsages { get; set; }

}


public class AppliedUsage
{
    public int SoftHoldID { get; set; }
    public int UsageYearID { get; set; }
    public int DaysApplied { get; set; }
    public int PointsApplied { get; set; }
}

尝试了下面的映射代码

ReservationDraftRequestDto reservationDto = null;

        dynamic data  = await reservationDraftRequestDto.Content.ReadAsAsync<object>();
                    reservationDto = JsonConvert.DeserializeObject<ReservationDraftRequestDto>(data.ToString());

【问题讨论】:

    标签: c# arrays json.net mapping automapper


    【解决方案1】:

    你需要改变

    dynamic data  = await reservationDraftRequestDto.Content.ReadAsAsync<object>();
    

    string data = await reservationDraftRequestDto.Content.ReadAsStringAsync();
    

    这会将您的响应读取为字符串

    然后做

    reservationDto = JsonConvert.DeserializeObject<ReservationDraftRequestDto>(data);
    

    【讨论】:

    • 不工作---执行函数时出现错误异常:Function1。 Newtonsoft.Json:无法将当前 JSON 对象(例如 {"name":"value"})反序列化为类型“testazfunction.AppliedUsage”,因为该类型需要 JSON 数组(例如 [1,2,3])才能正确反序列化。
    • 要修复此错误,要么将 JSON 更改为 JSON 数组(例如 [1,2,3]),要么将反序列化类型更改为正常的 .NET 类型(例如,不是原始类型像整数,而不是可以从 JSON 对象反序列化的集合类型(如数组或 List)。 JsonObjectAttribute 也可以添加到类型中,以强制它从 JSON 对象反序列化。
    • 尝试更改 public int[] Guest { get;放; } to public List Guest { get;放; } 和 public AppliedUsage[] AppliedUsages { get;放; } to public List AppliedUsages { get;放; } 在你的 DTO 中
    • 您是否检查过来自服务器的实际 json,可能是格式错误?
    【解决方案2】:

    这个作品

    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApp2
    {
             class Program
        {
            static void Main(string[] args)
            {
                string json = @"{""SoftHoldIDs"": 444,""AppliedUsages"": [    {""SoftHoldID"": 444,""UsageYearID"": 223232,""DaysApplied"": 0,""PointsApplied"": 1}],""Guests"": [ 1, 2]}";
    
                Rootobject reservationDto = JsonConvert.DeserializeObject<Rootobject>(json.ToString());
    
                Debug.WriteLine(reservationDto.SoftHoldIDs);
                foreach (var guest in reservationDto.Guests)
                {
    
                    Debug.WriteLine(guest);
                }
    
            }
        }
    
        public class Rootobject
        {
            public int SoftHoldIDs { get; set; }
            public Appliedusage[] AppliedUsages { get; set; }
            public int[] Guests { get; set; }
        }
    
        public class Appliedusage
        {
            public int SoftHoldID { get; set; }
            public int UsageYearID { get; set; }
            public int DaysApplied { get; set; }
            public int PointsApplied { get; set; }
        }
    
    
    }
    

    首先使用visualstudio创建类复制json作为类。 接下来,您在 json 响应中有双引号,因此请处理它。 Json.NET: Deserilization with Double Quotes

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-23
      • 1970-01-01
      • 2021-08-16
      • 1970-01-01
      • 2018-12-14
      • 1970-01-01
      • 2017-12-24
      • 2018-07-12
      相关资源
      最近更新 更多