I'm using Newtonsoft.Json on PCL project for a Xamarin.Android project.
This is how I used it:
var r = await _client.GetAsync("users/login?email=" + e+ "&password=" + p);
string c = await r.Content.ReadAsStringAsync();
var dtC= new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd hh:mi:ss:mmm" };
return string.IsNullOrEmpty(c) ? new Transporter<User>() :
JsonConvert.DeserializeObject<Transporter<User>>(c, dtC);
Transporter
is a json helper and its class is (short version):
public class Transporter<T>
{
public T data;
private bool success = true;
public Transporter()
{
}
public bool isSuccess()
{
return success;
}
public void setSuccess(bool success)
{
this.success = success;
}
public T getData()
{
return data;
}
public void setData(T data)
{
this.data = data;
}
}
And the User
class:
[Table("User")]
public class User
{
[PrimaryKey, AutoIncrement]
public long Id { get; set; }
public string Name { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public string Password { get; set; }
public DateTime CreationDate { get; set; }
}
All properties of User
comes right from the server. All filled up.
But when I do the deserialization - JsonConvert.DeserializeObject<Transporter<User>>(c, dtC)
- the property LastName
is null
and the property CreationDate
is 0001-01-01T00:00:00.0000000Z
.
EDIT:
{"data":{"id":2,"name":"test","last_name":"test 1","email":"test2","password":"aaa","creation_date":"2016-09-20T21:13:22.18"},"success":true}