我正在尝试构建一个模型来接收来自HTTPPOST的数据。
接收并填充模型 - IList<harsta> harequest
它显示为计数为1,但对字段具有空值:
我的模特是:
public class HAR
{
public int api_version { get; set; }
public IList<harsta> harequest { get; set; }
public class harsta
{
public int ta_id { get; set; }
public string partner_id { get; set; }
public string partner_url { get; set; }
}
...
...
}
harrequest的发布数据是(应该有2个条目):
[{"ta_id":97497,"partner_id":"229547","partner_url":"http://partner.com/deeplink/to/229547"},
{"ta_id":97832,"partner_id":"id34234","partner_url":"http://partner.com/deeplink/to/id34234"}]
PostMan的屏幕截图显示了发送到控制器的表单编码数据:
Example Request (this is the example provided on the 3rd party website)
POST
http://partner-site.com/api_implementation/ha
BODY
api_version=4
&harequest=[{"ta_id":97497,"partner_id":"229547","partner_url":"http://partner.com/deeplink/to/229547"},{"ta_id":97832,"partner_id":"id34234","partner_url":"http://partner.com/deeplink/to/id34234"}]
&start_date=2013-07-01
...
&query_key=6167a22d1f87d2028bf60a8e5e27afa7_191_13602996000
我确定它没有映射到我的模型,因为我在这里设置我的模型的方式:
public IList<harsta> harequest { get; set; }
public class harsta
{
public int ta_id { get; set; }
public string partner_id { get; set; }
public string partner_url { get; set; }
}
我是否错误地设置了模型,以便从POST中的harequest字段接收JSON数据?
答案 0 :(得分:-1)
首先,我对 Har 类中 Harsta 类的嵌入感到不太满意。不好的做法将它们分开。
其次,我认为您的问题实际上源于您返回的JSON对象中的属性名称用引号括起来。只删除属性名称的引号。
不要这样做:
[{"ta_id":97497,"partner_id":"229547","partner_url":"http://partner.com/deeplink/to/229547"},
{"ta_id":97832,"partner_id":"id34234","partner_url":"http://partner.com/deeplink/to/id34234"}]
请改为:
[{ta_id:97497,partner_id:"229547",partner_url:"http://partner.com/deeplink/to/229547"},
{ta_id:97832,partner_id:"id34234",partner_url:"http://partner.com/deeplink/to/id34234"}].