How to deserialize JSON Array in an Object Array?

Asked

Viewed 43 times

0

I’ve been trying for a while to deserialize a JSON return from my nodejs api. I’ve tried many ways and no success.

I have the following client request return JSON.

  "response": {
    "count": 1,
    "produtos": [
      {
        "id_produto": 1,
        "produto": "teste",
        "preco": 1455.99,
        "request": {
          "tipo": "GET",
          "descricao": "Retorna os detalhes de um produto específico.",
          "url": "http://localhost:3000/produtos/1"
        }
      }
    ]
  }
}

Already on the client side I have following codes:

I have a class for each equivalent object in json.

        //Representa o objeto 'response' em json
        public class ResponseObject
        {
            public List<ProdutoResponse> response { get; set; }

        }

        //Representa o array 'produtos' em json
        public class ProdutoResponse
        {
            public List<ListProdutos> produtos { get; set; }
        }

        //Especifica os campos do objetos dentro do array 'produtos' em json
        public class ListProdutos
        {
            public int id_produto { get; set; }
            public string produto { get; set; }
            public float preco { get; set; }
            public List<ListResquetStatus> request { get; set; }


        }
        //Especifica os campos dentro do objeto 'request'
        public class ListResquetStatus
        {
            public string tipo { get; set; }
            public string descricao { get; set; }
            public string url { get; set; }

        }

Deserializo o json: But it returns a null value

  static void JsonShowDes()
        {
            JavaScriptSerializer ser = new JavaScriptSerializer();

            var productsInfos = ser.Deserialize<List<ResponseObject>>(Get_json().ToString());           

        }

log

2 answers

1


I used the structure like this:

public class Request
{
    public string tipo { get; set; }
    public string descricao { get; set; }
    public string url { get; set; }
}

public class Produto
{
    public int id_produto { get; set; }
    public string produto { get; set; }
    public double preco { get; set; }
    public Request request { get; set; }
}

public class Response
{
    public int count { get; set; }
    public List<Produto> produtos { get; set; }
}

public class ResponseObject
{
    public Response response { get; set; }
}

To test I used the Newtonsoft, because testing using a class and this package is simple to install, and successfully deserializes:

var productsInfos = Newtonsoft.Json.JsonConvert.DeserializeObject<ResponseObject>(json);

There are two differences, from your model, 1 which, by json, does not serialize:
In your class ResponseObject has a List<ProdutoResponse>, where json, "respose" is an object, not an array (if it comes one more, it should be an Dictiornary). Besides, it lacked the property Count in ProdutoResponse.

You can see it working here: https://dotnetfiddle.net/FcBpZ0

  • Mano vc is too much, our worked perfectly. Thank you very much ;) hugging

0

Alter your ResponseObject for:

public class ResponseObject
{
    //public int count { get; set; }
    
    public ProdutoResponse response { get; set; }
}

Also note that you are using id_produtos (with s at the end) in your class ListProdutos, and in json is only id_produto.

  • Confused here, I’ll change the answer.

  • Thanks for the correction. I made the proper change in the code, but nothing has changed, I get null yet

  • I changed it again.

Browser other questions tagged

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