0

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}
lpfx
  • 1,476
  • 4
  • 18
  • 40
  • 2
    Any chance you show us the raw JSON data? Scientific guess: camel case. – zerkms Oct 09 '16 at 20:57
  • I agree with @zerk, without that we cannot say for sure what the problem is. My guess is that the JSON has a properties are a different case than your model class, i.e. `CreationDate` is not the same as `creationdate` – DavidG Oct 09 '16 at 21:01
  • @zerkms, see my edit. – lpfx Oct 10 '16 at 01:38
  • Well, my guess was correct. – zerkms Oct 10 '16 at 01:47
  • 1
    Possible duplicate of [.Net NewtonSoft Json Deserialize map to a different property name](http://stackoverflow.com/questions/15915503/net-newtonsoft-json-deserialize-map-to-a-different-property-name) – zerkms Oct 10 '16 at 01:49
  • That was it, thanks! Please put it as an answer so I can accept it. – lpfx Oct 10 '16 at 02:34

1 Answers1

0

The format yyyy-MM-dd hh:mi:ss:mmm is invalid, because it has the colon : just before the milliseconds part. Should it be yyyy-MM-dd hh:mi:ss.mmm.

Serge Semenov
  • 9,232
  • 3
  • 23
  • 24