Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/facebook/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在WEB API 2中接收对象JSON_C#_Json_Asp.net Web Api2 - Fatal编程技术网

C# 在WEB API 2中接收对象JSON

C# 在WEB API 2中接收对象JSON,c#,json,asp.net-web-api2,C#,Json,Asp.net Web Api2,如何使用Web Api 2接收对象json。 我正确发送了对象,但在后端,我的对象为空。 见下文 我的对象JSON 这是我的JSON对象,将在请求中发送 { "ItensRateio" : [ { "TipoColetorCusto" : "1", "ColetorCusto" : "MRBHAD", "Valor": "R$ 25.22", "Descricao": "Rat

如何使用Web Api 2接收对象json。 我正确发送了对象,但在后端,我的对象为空。 见下文

我的对象JSON

这是我的JSON对象,将在请求中发送

{
    "ItensRateio" : [
        {
            "TipoColetorCusto" : "1",
            "ColetorCusto" : "MRBHAD",
            "Valor": "R$ 25.22",
            "Descricao": "Rateio do Brasil"
        },
        {
            "TipoColetorCusto" : "1",
            "ColetorCusto" : "MRBHAD",
            "Valor": "R$ 25.22",
            "Descricao": "Rateio do Brasil"
        }
    ]
}
public class RateioSimplesRequestModel
{
    List<ItemRateio> ItensRateio { get; set; }
    List<ItemMaterial> ItensMaterial { get; set; }

    public RateioSimplesRequestModel()
    {
        ItensRateio = new List<ItemRateio>();
        ItensMaterial = new List<ItemMaterial>();
    }
}

public class ItemRateio
{
    public string TipoColetorCusto { get; set; }
    public string ColetorCusto { get; set; }
    public string Valor { get; set; }
    public string Descricao { get; set; }
}

public class ItemMaterial
{
    public string CNAE { get; set; }
    public string CodigoMaterial { get; set; }
    public string Descricao { get; set; }
}
我的目标

这是我的映射对象。需要使用请求中接收的JSON对象填充的类

{
    "ItensRateio" : [
        {
            "TipoColetorCusto" : "1",
            "ColetorCusto" : "MRBHAD",
            "Valor": "R$ 25.22",
            "Descricao": "Rateio do Brasil"
        },
        {
            "TipoColetorCusto" : "1",
            "ColetorCusto" : "MRBHAD",
            "Valor": "R$ 25.22",
            "Descricao": "Rateio do Brasil"
        }
    ]
}
public class RateioSimplesRequestModel
{
    List<ItemRateio> ItensRateio { get; set; }
    List<ItemMaterial> ItensMaterial { get; set; }

    public RateioSimplesRequestModel()
    {
        ItensRateio = new List<ItemRateio>();
        ItensMaterial = new List<ItemMaterial>();
    }
}

public class ItemRateio
{
    public string TipoColetorCusto { get; set; }
    public string ColetorCusto { get; set; }
    public string Valor { get; set; }
    public string Descricao { get; set; }
}

public class ItemMaterial
{
    public string CNAE { get; set; }
    public string CodigoMaterial { get; set; }
    public string Descricao { get; set; }
}
公共类RateIOSimpleRequestModel
{
列出ItensRateio{get;set;}
列出ItensMaterial{get;set;}
公共费率IMPLESRequestModel()
{
ItensRateio=新列表();
ItensMaterial=新列表();
}
}
公共类项目费率
{
公共字符串TipoColetorCusto{get;set;}
公共字符串ColetorCusto{get;set;}
公共字符串值{get;set;}
公共字符串描述符{get;set;}
}
公共类项目材料
{
公共字符串CNAE{get;set;}
公共字符串{get;set;}
公共字符串描述符{get;set;}
}
我在WebAPi 2中的方法

[路径(“计算速率手动”)]
[HttpPost]
public RespostaPadrao CalcularRateiManual([FromBody]RateIOSimpleRequestModel Parameter)//此对象
{
RespostaPadrao returno=新RespostaPadrao();
返回号;
}


我怎么能做到完美呢?

您的列表不是公共的,默认情况下json.net只映射公共属性。此外,集合不应是可公共设置的

public class RateioSimplesRequestModel
{
    public List<ItemRateio> ItensRateio { get; private set; }
    public List<ItemMaterial> ItensMaterial { get; private set; }

    public RateioSimplesRequestModel()
    {
        ItensRateio = new List<ItemRateio>();
        ItensMaterial = new List<ItemMaterial>();
    }
}
公共类RateIOSimpleRequestModel
{
公共列表ItensRateio{get;private set;}
公共列表ItensMaterial{get;private set;}
公共费率IMPLESRequestModel()
{
ItensRateio=新列表();
ItensMaterial=新列表();
}
}

您的列表不是公共的,默认情况下json.net只映射公共属性。此外,集合不应是可公共设置的

public class RateioSimplesRequestModel
{
    public List<ItemRateio> ItensRateio { get; private set; }
    public List<ItemMaterial> ItensMaterial { get; private set; }

    public RateioSimplesRequestModel()
    {
        ItensRateio = new List<ItemRateio>();
        ItensMaterial = new List<ItemMaterial>();
    }
}
公共类RateIOSimpleRequestModel
{
公共列表ItensRateio{get;private set;}
公共列表ItensMaterial{get;private set;}
公共费率IMPLESRequestModel()
{
ItensRateio=新列表();
ItensMaterial=新列表();
}
}

请注意,在
类的属性中有
列表
。。。因此,json对象需要正确格式化,并且应该发送
parametro
,而不是
ItensRateio

 var parametro = {};
 parametro.ItensRateio = [
   {
     "TipoColetorCusto" : "1",
     "ColetorCusto" : "MRBHAD",
     "Valor": "R$ 25.22",
     "Descricao": "Rateio do Brasil"
   },
   {
     "TipoColetorCusto" : "1",
     "ColetorCusto" : "MRBHAD",
     "Valor": "R$ 25.22",
     "Descricao": "Rateio do Brasil"
   }
 ];

请注意,您在
类的属性中有
列表
。。。因此,json对象需要正确格式化,并且应该发送
parametro
,而不是
ItensRateio

 var parametro = {};
 parametro.ItensRateio = [
   {
     "TipoColetorCusto" : "1",
     "ColetorCusto" : "MRBHAD",
     "Valor": "R$ 25.22",
     "Descricao": "Rateio do Brasil"
   },
   {
     "TipoColetorCusto" : "1",
     "ColetorCusto" : "MRBHAD",
     "Valor": "R$ 25.22",
     "Descricao": "Rateio do Brasil"
   }
 ];

正如Oliver所提到的,公开列表应该允许将其转换为对象

如果使用C#,验证对象是否可以序列化的一种方法是构造对象并使用JsonConvert将其转换为JSON等价物。这将突出显示任何成员保护级别问题,并生成预期的JSON

基于上述类,这将生成以下JSON:

{
  "ItensRateio": [
    {
      "TipoColetorCusto": "1",
      "ColetorCusto": "MRBHAD",
      "Valor": "R$ 25.22",
      "Descricao": "Rateio do Brasil"
    },
    {
      "TipoColetorCusto": "1",
      "ColetorCusto": "MRBHAD",
      "Valor": "R$ 25.22",
      "Descricao": "Rateio do Brasil"
    }
  ],
  "ItensMaterial": []
}

正如Oliver所提到的,公开列表应该允许将其转换为对象

如果使用C#,验证对象是否可以序列化的一种方法是构造对象并使用JsonConvert将其转换为JSON等价物。这将突出显示任何成员保护级别问题,并生成预期的JSON

基于上述类,这将生成以下JSON:

{
  "ItensRateio": [
    {
      "TipoColetorCusto": "1",
      "ColetorCusto": "MRBHAD",
      "Valor": "R$ 25.22",
      "Descricao": "Rateio do Brasil"
    },
    {
      "TipoColetorCusto": "1",
      "ColetorCusto": "MRBHAD",
      "Valor": "R$ 25.22",
      "Descricao": "Rateio do Brasil"
    }
  ],
  "ItensMaterial": []
}

就这样!如此简单但如此重要的事情。非常感谢你帮助我,我的朋友。你很聪明。拥抱,就是这样!如此简单但如此重要的事情。非常感谢你帮助我,我的朋友。你很聪明。拥抱。