Tea With Cookies
Tea With Cookies

Reputation: 1020

Serializing/Deserializing List into JSON

I'm trying to serialize/deserialize a list of objects across two projects I have. This is the class I'm using:

public class Stock
{
    public string Bin { get; set; }
    public string Article { get; set; }
    public int Quantity { get; set; }
}

The part where I serialize:

var msg = JsonConvert.SerializeObject(result.Select(k => new Stock() { Article = article, Bin = k.Bin, Quantity = k.Quantity }));

The part where I deserialize:

var stocks = JsonConvert.DeserializeObject<IEnumerable<Stock>>(data);

This is the exception I get:

ArgumentException: Could not cast or convert from System.String to System.Collections.Generic.IEnumerable`1[DTOs.Stock].

This is the data that is displayed on the error page:

[{"Bin":"MJ11906","Article":"148400MU_","Quantity":50},{"Bin":"ME04307","Article":"148400MU_","Quantity":56},{"Bin":"MD08301","Article":"148400MU_","Quantity":66},{"Bin":"MG12303","Article":"148400MU_","Quantity":70},{"Bin":"ME04402","Article":"148400MU_","Quantity":72},{"Bin":"ME12402","Article":"148400MU_","Quantity":72}]

I can't find what I'm doing wrong, I've tried to serialize/deserialize to/from List and Ienumerable but the result is the same. Can't seem to find any Google result to solve this...

Upvotes: 0

Views: 225

Answers (1)

Rui Jarimba
Rui Jarimba

Reputation: 18129

I just created a .NET Core 2.0 console application using VS2017 (Community Edition). Code is the following:

using System;
using System.Collections.Generic;

using Newtonsoft.Json;

namespace JsonDemo
{
    public class Stock
    {
        public string Bin { get; set; }
        public string Article { get; set; }
        public int Quantity { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            string json = @"[{""Bin"":""MJ11906"",""Article"":""148400MU_"",""Quantity"":50},{""Bin"":""ME04307"",""Article"":""148400MU_"",""Quantity"":56},{""Bin"":""MD08301"",""Article"":""148400MU_"",""Quantity"":66},{""Bin"":""MG12303"",""Article"":""148400MU_"",""Quantity"":70},{""Bin"":""ME04402"",""Article"":""148400MU_"",""Quantity"":72},{""Bin"":""ME12402"",""Article"":""148400MU_"",""Quantity"":72}]";

            var stocks = JsonConvert.DeserializeObject<IEnumerable<Stock>>(json);
            var stocks2 = JsonConvert.DeserializeObject<IList<Stock>>(json);
            var stocks3 = JsonConvert.DeserializeObject<List<Stock>>(json);
            var stocks4 = JsonConvert.DeserializeObject<Stock[]>(json);

            string json2 = JsonConvert.SerializeObject(stocks);

            stocks = JsonConvert.DeserializeObject<IEnumerable<Stock>>(json);
            stocks2 = JsonConvert.DeserializeObject<IList<Stock>>(json);
            stocks3 = JsonConvert.DeserializeObject<List<Stock>>(json);
            stocks4 = JsonConvert.DeserializeObject<Stock[]>(json);

            Console.WriteLine("Press any key to continue");
            Console.Read();
        }
    }
}

No exceptions are being thrown. I have tried with versions 9.0.1, 10.0.1 and 11.0.2 of nuget package Newtonsoft.Json, everything works as expected.

Have you tried using updating the version of Newtonsoft.Json?

Upvotes: 1

Related Questions