Error when deserializing a Json that I receive from the server

1
  

Can not deserialize the current JSON array (eg [1,2,3]) into type 'Gnaritas.Colour.Models.ColumnForm' because the type requires a JSON object (eg {"name": "value"}) to deserialize correctly.

     

To fix this error, either change the JSON to a JSON object (eg {"name": "value"}) or change the deserialized type to an array that implements a collection interface (eg ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.
  Path '', line 1, position 1.

I'm developing in C #

My class that performs deserialization:

public List<Coleta> SolicitarResumo()
    {
        List<CampoFormulario> dados = new List<CampoFormulario>();

        dados.Add(new CampoFormulario(TokenService.ObterCodUser(), "usuar-sep") { dataType = "character", type = "input" });
        //dados.Add(new CampoFormulario(DateTime.Today.ToShortDateString(), "data-sep") { type = "input", dataType = "datetime" });

        CampoFormularioColecao campoColecao = new CampoFormularioColecao("valor");
        campoColecao.type = "output";
        campoColecao.value.fields.Add(new Field("cdd-embarq", "Codigo") { type = "integer" });
        campoColecao.value.fields.Add(new Field("nome-abrev", "Cliente"));
        campoColecao.value.fields.Add(new Field("vl-cliente", "Valor") { type = "decimal" });

        dados.Add(campoColecao);

        string dadosJson = JsonConvert.SerializeObject(dados);

        callProcedureWithToken chamada = new callProcedureWithToken()
        {
            arg0 = TokenService.ObterToken(),
            arg1 = "esp/esae0000.p",
            arg2 = "pi-retorna-usuar-valor",
            arg3 = dadosJson
        };

        callProcedureWithTokenResponse resposta = cliente.callProcedureWithToken(chamada);

        CampoFormularioColecao registrosDicionario = JsonConvert.DeserializeObject<CampoFormularioColecao>(resposta.@return);

Data type for which I want to deserialize json:

 public class CampoFormularioColecao : CampoFormulario
{
    public CampoFormularioColecao(string tipo)
    {
        dataType = "temptable";
        name = "tt-lista-" + tipo;
        value = new Value() { name = this.name };
    }

    public new Value value { get; set; }
}

public class Value
{
    public Value()
    {
        fields = new List<Field>();
        records = new List<JObject>();
    }

    public List<Field> fields { get; set; }

    public List<JObject> records { get; set; }

    public string name { get; set; }
}

public class Field
{
    public Field(string pname, string plabel)
    {
        label = plabel;
        name = pname;
        type = "character";
    }
    public string name { get; set; }
    public string label { get; set; }
    public string type { get; set; }
}

This is Json that I get:

[{
"dataType": "temptable",
"name": "tt-lista-valor",
"value": "{\"records\":[{\"vl-cliente\":240.12,\"cdd-embarq\":341647,\"nome-abrev\":\"MULT CHAVES\"},{\"vl-cliente\":1906.0,\"cdd-embarq\":341647,\"nome-abrev\":\"MULT CHAVES\"},{\"vl-cliente\":240.12,\"cdd-embarq\":341647,\"nome-abrev\":\"MULT CHAVES\"},{\"vl-cliente\":345.78,\"cdd-embarq\":341647,\"nome-abrev\":\"MULT CHAVES\"}],\"fields\":[{\"name\":\"cdd-embarq\",\"label\":\"Codigo\",\"type\":\"integer\"},{\"name\":\"nome-abrev\",\"label\":\"Cliente\",\"type\":\"character\"},{\"name\":\"vl-cliente\",\"label\":\"Valor\",\"type\":\"decimal\"}]}",
"type": "output"

}]

It seems that it does not recognize the type CampoFormularioColecao ... Well, like I said, I'm new here, I apologize if I was not clear enough or if I asked for something trivial

    
asked by anonymous 08.11.2017 / 22:12

1 answer

2

Json posted above is experiencing training issues. Below you can find an example code that deserializes the Json formatted correctly.

The model classes:

public class MinhaClasse
{
    public string dataType { get; set; }
    public string name { get; set; }
    public Value value { get; set; }
    public string type { get; set; }
}

public class Value
{
    public List<Record> records { get; set; }
    public List<Field> fields { get; set; }
}

public class Record
{
    [JsonProperty("vl-cliente")]
    public string vlcliente { get; set; }
    [JsonProperty("cdd-embarq")]
    public string cddembarq { get; set; }
    [JsonProperty("nome-abrev")]
    public string nomeabrev { get; set; }
}

public class Field
{
    public string name { get; set; }
    public string label { get; set; }
    public string type { get; set; }
}

And the deserialization of Json:

    static void Main(string[] args)
    {
        string json = @"[{""dataType"":""temptable"",""name"":""tt-lista-valor"",""value"":{""records"":[{""vl-cliente"":240.12,""cdd-embarq"":341647,""nome-abrev"":""MULT CHAVES""},{""vl-cliente"":1906.0,""cdd-embarq"":341647,""nome-abrev"":""MULT CHAVES""},{""vl-cliente"":240.12,""cdd-embarq"":341647,""nome-abrev"":""MULT CHAVES""},{""vl-cliente"":345.78,""cdd-embarq"":341647,""nome-abrev"":""MULT CHAVES""}],""fields"":[{""name"":""cdd-embarq"",""label"":""Codigo"",""type"":""integer""},{""name"":""nome-abrev"",""label"":""Cliente"",""type"":""character""},{""name"":""vl-cliente"",""label"":""Valor"",""type"":""decimal""}]},""type"":""output""}]";
        var obj = Newtonsoft.Json.JsonConvert.DeserializeObject<IEnumerable<MinhaClasse>>(json);
    }

For this example we used the NewtonSoft library that can be obtained through NuGet.

    
10.11.2017 / 02:18