1

Here's how I try to deserialize my json:

new JavaScriptSerializer().Deserialize<Dictionary<int, MyModel>>(myData);

Here's the class:

public class MyModel
{
    public Dictionary<int, ItemModel> Translation { get; set; }

    public int Id { get; set; }
}

public class ItemModel
{
    public string Name { get; set; }

    public string ShortDescription { get; set; }

    public string LongDescription { get; set; }
}

And here's the json:

"[[],[],{"Translation":{"1":{"Name":"Bla1","ShortDescription":"bla1","LongDescription":"bla1"},"2":{"Name":"BlaUS1","ShortDescription":"BlaUS1","LongDescription":"BlaUS1"}},"Id":"12"},{"Translation":{"1":{"Name":"Bla22","ShortDescription":"bla22","LongDescription":"bla22"},"2":{"Name":"Bla2US2","ShortDescription":"Bla2US2","LongDescription":"Bla2US2"}},"Id":"13"}]"

and I get the error that the type is not supported for deserialization of an array.

Where is my error?

petko_stankoski
  • 10,459
  • 41
  • 127
  • 231
  • You appear to have an array where the first two elements are 0-length arrays and the 3rd and 4th elements are of MyModel - it probably can't deserialize as it can't turn the empty array into MyModel, set the first two elements to `null` rather than `[]` – Shaun Wilde Nov 03 '14 at 10:59

2 Answers2

1

First of all your JSON looks a bit wrong to me. It is and array of 4 elements and 1st two elements are empty arrays but other two objects? I suspect your JSON should be something like that:

"[{"Translation":{"1":{"Name":"Bla1","ShortDescription":"bla1","LongDescription":"bla1"},"2":{"Name":"BlaUS1","ShortDescription":"BlaUS1","LongDescription":"BlaUS1"}},"Id":"12"},{"Translation":{"1":{"Name":"Bla22","ShortDescription":"bla22","LongDescription":"bla22"},"2":{"Name":"Bla2US2","ShortDescription":"Bla2US2","LongDescription":"Bla2US2"}},"Id":"13"}]"

Another issue is that you have Dictionary<int, ItemModel> but for serialization/deserialization you must have key of String or Object type.

Working example (providing that you changed from Dictionary<int, ItemModel> to Dictionary<object, ItemModel>):

string input = "[{\"Translation\":{\"1\":{\"Name\":\"Bla1\",\"ShortDescription\":\"bla1\",\"LongDescription\":\"bla1\"},\"2\":{\"Name\":\"BlaUS1\",\"ShortDescription\":\"BlaUS1\",\"LongDescription\":\"BlaUS1\"}},\"Id\":\"12\"},{\"Translation\":{\"1\":{\"Name\":\"Bla22\",\"ShortDescription\":\"bla22\",\"LongDescription\":\"bla22\"},\"2\":{\"Name\":\"Bla2US2\",\"ShortDescription\":\"Bla2US2\",\"LongDescription\":\"Bla2US2\"}},\"Id\":\"13\"}]";

List<MyModel> myModels = new JavaScriptSerializer().Deserialize<List<MyModel>>(input);
Vladimirs
  • 8,232
  • 4
  • 43
  • 79
0

Your string suggests that what you have is a JSON array, eg:- [1,2,3]

but you are trying to deserialize it into a dictionary for which the json representation is akin to

{"1":"Hai","2":"Hello"}

obviously the library is throwing an error. May be why dont you use the following to deserialize the string.

new JavaScriptSerializer().Deserialize<List<MyModel>[]>(myData)

However, to use it you can't have empty arrays in the json, you have to fill them with default values for the properties.

To prove that the above works, try

"[{"Translation":{"1":{"Name":"Bla1","ShortDescription":"bla1","LongDescription":"bla1"},"2":  {"Name":"BlaUS1","ShortDescription":"BlaUS1","LongDescription":"BlaUS1"}},"Id":"12"},{"Translation":{"1":{"Name":"Bla22","ShortDescription":"bla22","LongDescription":"bla22"},"2":{"Name":"Bla2US2","ShortDescription":"Bla2US2","LongDescription":"Bla2US2"}},"Id":"13"}]"

with

new JavaScriptSerializer().Deserialize<List<MyModel>>(myData)
csprabala
  • 177
  • 12
  • @csprabala I tried with List[] and it gives me a list with size of 2, but with empty objects. List doesn't work, it throws an exception. – petko_stankoski Nov 03 '14 at 10:15
  • @PatrickHofman is right. This won't work. Take a look at this. http://stackoverflow.com/questions/16015257/why-does-c-sharp-javascriptserializer-serialize-return-empty-square-brackets – csprabala Nov 03 '14 at 10:39
  • I guess you have to resort to using a different library to deserialize. – csprabala Nov 03 '14 at 10:40