【问题标题】:Print Json object using C#使用 C# 打印 Json 对象
【发布时间】:2021-10-15 16:41:46
【问题描述】:

我想返回这个 json 响应

{
    "Type": "testing",
    "Errors": {
        "InputOne": [
            "The input one is accepted"
        ],
        "InputTwo": [
            "The input two is accepted"
        ]
    }
}

但在尝试了几轮之后,我得到的只是这个

{
    "Type": "testing",
    "Errors": {
        "InputOne": "The input one is accepted",
        "InputTwo": "The input two is accepted"
    }
}

我可以知道我在这里缺少什么吗?

这些是我的代码

public string Type { get; set; }
public ErrorClass Errors { get; set; }

public class ErrorClass
{
  public object InputOne { get; set; }
  public object InputTwo { get; set; }
}

提前致谢!

【问题讨论】:

标签: c# asp.net json asp.net-mvc asp.net-core


【解决方案1】:

试试这个:您需要根据您的响应返回一个 InputOneInputTwo 的列表/集合/数组。

public string Type { get; set; }
public ErrorClass Errors { get; set; }

public class ErrorClass
{
   public List<object> InputOne { get; set; }
 public List<object> InputTwo { get; set; }
}

【讨论】:

    【解决方案2】:

    试试这个:

    public string Type { get; set; }
    public ErrorClass Errors { get; set; }
    
    public class ErrorClass
    {
      public string[] InputOne { get; set; }
      public string[] InputTwo { get; set; }
    }
    

    【讨论】:

    • 感谢您的帮助 nicola,在@patrickartner 评论从 json 生成 c 类文件后,我最终也发现了非常感谢你们!
    猜你喜欢
    • 2013-06-21
    • 2021-04-16
    • 2018-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-24
    • 2020-11-15
    • 1970-01-01
    相关资源
    最近更新 更多