reviloRetals
reviloRetals

Reputation: 525

Receiving JSON data on Web API Visual Studio C#

I have a web api which is receiving some POST data. I am able to parse the JSON string when the elements are not nested - but the nested ones just will not parse at all...

Here is the JSON I am receiving:

{
    "wlauth": {
        "userid": "user",
        "password": "pass"
    },
    "ident": "01234567890",
    "identtype": "imsi",
    "message": "VGVzdCBNZXNzYWdl"
}

Here is the code from the Controller that handles the Post request:

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();
}

From this I get the following in the debug console (the first two lines are blank):

'


01234567890
imsi
VGVzdCBNZXNzYWdl'

So in other words, the non-nested elements appear fine, but the nested ones do not - what should I be doing differently to retrieve those nested elements?

Edit:

Here is the SMSReturned Class:

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; }

}

Upvotes: 0

Views: 1258

Answers (1)

Theo
Theo

Reputation: 885

The structure for SMSReturned is missing some elements. Try this:

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; }

}

and this:

 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();
 }

Upvotes: 3

Related Questions