C# 在Web API Visual Studio C上接收JSON数据#

C# 在Web API Visual Studio C上接收JSON数据#,c#,json,asp.net-web-api2,C#,Json,Asp.net Web Api2,我有一个web api,它正在接收一些POST数据。 当元素没有嵌套时,我可以解析JSON字符串,但是嵌套的元素根本不会解析 以下是我收到的JSON: { "wlauth": { "userid": "user", "password": "pass" }, "ident": "01234567890", "identtype": "imsi", "message": "VGVzdCBNZXNzYWdl" } 以下是处理P

我有一个web api,它正在接收一些POST数据。 当元素没有嵌套时,我可以解析JSON字符串,但是嵌套的元素根本不会解析

以下是我收到的JSON:

{
    "wlauth": {
        "userid": "user",
        "password": "pass"
    },
    "ident": "01234567890",
    "identtype": "imsi",
    "message": "VGVzdCBNZXNzYWdl"
}
以下是处理Post请求的控制器的代码:

public IHttpActionResult ReceiveSMSData(SMSReturned data)
{
 Debug.WriteLine(data.userid);
 Debug.WriteLine(data.password);
 Debug.WriteLine(data.Ident);
 Debug.WriteLine(data.identtype);
 Debug.WriteLine(data.message);
 return Ok();
}
由此,我在调试控制台中获得以下内容(前两行为空):

换句话说,非嵌套的元素看起来很好,但嵌套的元素不行-我应该做些什么来检索那些嵌套的元素呢

编辑:

以下是SMSReerned类:

public class SMSReturned
{
    public string wlauth { get; set; }
    public string Ident { get; set; }
    public string identtype { get; set; }
    public string message { get; set; }
    public string userid { get; set; }
    public string password { get; set; }

}

SMSREETURN
的结构缺少某些元素。试试这个:

public class WLAuth
{
    public string userid { get; set; }
    public string password { get; set; }
} 
public class SMSReturned
{

    public WLAuth wlauth { get; set; }
    public string Ident { get; set; }
    public string identtype { get; set; }
    public string message { get; set; }
    public string userid { get; set; }
    public string password { get; set; }

}
这是:

 public IHttpActionResult ReceiveSMSData(SMSReturned data)
 {
    Debug.WriteLine(data.wlauth.userid);
    Debug.WriteLine(data.wlauth.password);
    Debug.WriteLine(data.Ident);
    Debug.WriteLine(data.identtype);
    Debug.WriteLine(data.message);
    return Ok();
 }

SMSREETURN
的结构缺少某些元素。试试这个:

public class WLAuth
{
    public string userid { get; set; }
    public string password { get; set; }
} 
public class SMSReturned
{

    public WLAuth wlauth { get; set; }
    public string Ident { get; set; }
    public string identtype { get; set; }
    public string message { get; set; }
    public string userid { get; set; }
    public string password { get; set; }

}
这是:

 public IHttpActionResult ReceiveSMSData(SMSReturned data)
 {
    Debug.WriteLine(data.wlauth.userid);
    Debug.WriteLine(data.wlauth.password);
    Debug.WriteLine(data.Ident);
    Debug.WriteLine(data.identtype);
    Debug.WriteLine(data.message);
    return Ok();
 }

我们需要看看你们的模型。还请注意,
visual studio
标记用于有关VS的问题,而不是编码问题。发布
SMSReentern
类的代码。是的,我们需要查看类这里是SMSReentern类的代码:`public class smsreentern{public string wlauth{get;set;}public string Ident{get;set;}公共字符串标识类型{get;set;}公共字符串消息{get;set;}公共字符串用户ID{get;set;}公共字符串密码{get;set;}'wlauth是json中的一个对象,而不是一个字符串。您需要创建另一个表示wlauth的模型,并将其作为SMSReturned类的属性。我们需要查看您的模型。还要注意,
visual studio
标记用于有关VS的问题,而不是编码问题。发布
SMSReturned
类的代码。是的,我们需要查看类这里是SMSReturned类的代码:`public class SMSReturned{public string wlauth{get;set;}public string Ident{get;set;}public string identtype{get;set;}public string message{get;set;}public string userid{get;set;}public string password{get;set;}'wlauth是json中的一个对象,而不是一个字符串。您需要创建另一个表示wlauth的模型,并将其作为SMS返回类的属性。默认情况下,Web API使用Newtonsoft。要使用
DataContractJsonSerializer
,必须手动配置
JsonMediaTypeFormatter
。MVC使用
DataContractJsonSer默认情况下,ializer
。Web API默认使用Newtonsoft。要使用
DataContractJsonSerializer
,必须手动配置
JsonMediaTypeFormatter
。默认情况下,MVC使用
DataContractJsonSerializer