0

I'm building a xamarin application that connects to an API that I built and it's running fine. When I do the call to the API on xamarin I get this error:

Newtonsoft.Json.JsonSerializationException: Unable to find a constructor to use for type GesfrotaTrajetos.Models.API.Root. A class should either have a default constructor, one constructor with arguments or a constructor marked with the JsonConstructor attribute. Path 'idCondutor', line 1, position 14.

I've already added the constructor and marked them with the attribute JsonConstructor, already went to the Android project properties and set the linking to Sdk and User Assemblies, and added the Newtonsoft.Json to the Skip linking Assemblies.

What am I doing wrong?

Here is my Root class:

public class Root
{   
    [JsonConstructor]
    public Root() { }
    public int idCondutor { get; set; }
    public string nomeCondutor { get; set; }
    public List<ListRota> listRotas { get; set; }
    public RotaEmAbertoCondutor rotaEmAbertoCondutor { get; set; }
    public ViaturaPreferencial viatura_preferencial { get; set; }
}


public class Viatura
{   
    public Viatura() { }
    public int id { get; set; }
    public string matricula { get; set; }
}

public class ListRota
{ 
    public ListRota() { }
    public int id { get; set; }
    public string nome { get; set; }
    public double distancia_ideal { get; set; }
    public List<Viatura> viaturas { get; set; }
}

public class RotaEmAbertoCondutor
{
    public RotaEmAbertoCondutor() { }
    public int idRota { get; set; }
    public object nomeRota { get; set; }
    public int idViatura { get; set; }
    public object matriculaViatura { get; set; }
    public int idCircuito { get; set; }
    public DateTime inicio { get; set; }
}

public class ViaturaPreferencial
{   
    public ViaturaPreferencial() { }
    public int id { get; set; }
    public int id_condutor { get; set; }
    public int id_viatura { get; set; }
}
6
  • I would try getting rid of all the default constructors, they should not be needed. If you still get the error after that, please post a sample of the json Commented Mar 7, 2022 at 15:31
  • @Jason did that and got the same error... here is my JSON { "idCondutor": 1, "nomeCondutor": "driver1", "listRotas": [{ "id": 1, "nome": "rota2", "distancia_ideal": 0.00, "viaturas": [{ "id": 1, "matricula": "AA-00-BB" } }] }], "rotaEmAbertoCondutor": { "idRota": 0, "nomeRota": null, "idViatura": 0, "matriculaViatura": null, "idCircuito": 0, "inicio": "2022-03-07T15:39:20.1957375+00:00" }, "viatura_preferencial": { "id": 0, "id_condutor": 0, "id_viatura": 0 } } Commented Mar 7, 2022 at 15:43
  • 1
    Adding JsonConstructorAttribute isn't going to help one way or another since your class has an explicit public parameterless constructor yet somehow Json.NET can't find it, which suggests it has been removed by xamarin during the optimization phase. The fix suggested in the linked questions was to use PreserveAttribute; did that work? Commented Mar 7, 2022 at 15:46
  • 1
    @dbc it did, I had to added the attribute to all sub-classes but yes, it works. Thanks! Commented Mar 7, 2022 at 18:07

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.