Error deserializing a Json I receive from the server

Asked

Viewed 2,998 times

1

Cannot deserialize the Current JSON array (e.g. [1,2,3]) into type 'Gnaritas.Coletor.Templates.Fieldfieldcollection' because the type requires a JSON Object (e.g. {"name":"value"}) to deserialize correctly.

To fix this error either change the JSON to a JSON Object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that Implements a Collection interface (e.g. 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 wish 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 the Json 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 you don’t recognize the type Campoformulariocolecao ... Well, like I said, I’m new here, I apologize if I wasn’t clear enough or if I asked for something trivial

1 answer

2


The Json posted above is having training problems. Below you can find an example of code that deserializes the properly formatted Json.

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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.