1

I have a JSON-string with a fixed number of arrays of different objects (created in Java with JSONObjects and JSONArrays):

[
  [ //The first type of object
    {
      "Initials":"MUS"
    },
    {
      "Initials":"NA"
    }
  ],
  [ //The second type
    {
      "ToolId":17
    },
    {
      ...
    }
  ]
  ... //etc.
]

So I've created some Dummy-classes that has corresponding properties to the objects within the array, which works:

private class DummyEmployee
{
    public string Initials { get; set; }
}
//etc.

But I can't figure out how the container class should be designed. This is how I did it:

private class DataContainer
{
    public List<DummyEmployee> Employees { get; set; }
    public List<DummySecondType> SecondTypes { get; set; }
    //etc.
}

This is how I attempt to deserialize the JSON-data:

JavaScriptSerializer ser = new JavaScriptSerializer();

string jsonDataFromClient = ...;

DataContainer jsonData = ser.Deserialize<DataContainer>(jsonDataFromClient);

And it doesn't work. I get the following error while passing the data: Type 'GUI.ValidateLoginData+DataContainer' is not supported for deserialization of an array.

I couldn't find any other subjects on the matter of deserializing arrays of different objects.

1
  • stackoverflow.com/questions/2962551/… how are you deserializing it, with JSON.net, JavaScriptSerializer... Could you post the code of the Deserialization.... Commented May 30, 2012 at 11:41

3 Answers 3

2

yes i will not work
notice that in your javascript object is basically an array and because javascript is a dynamic language there it is a valid array
where as c# isnt so an array(or list) must contain objects of same kind. however if you still need to implement this and you have control over your JSON structure edit it to this

{
  Employees:[ //The first type of object
    {
      "Initials":"MUS"
    },
    {
      "Initials":"NA"
    }
  ],
  SecondTypes:[ //The second type
    {
      "ToolId":17
    },
    {
      ...
    }
  ]
  ... //etc.
}

and your current c# object might map correctly.

and if you dont have control over the JSON structure then you have to use dynamic objects in c#
UPDATE:-for the case in which you dont have control over your JSON structure (or you dont wanna edit it).
try deserializing your JSON object to an array of dynamic type
UPDATE 2:- because you are curious try deserializing the existing JSON structure to an object of type List<List<dynamic>> and though i havent tried it but it should work fine.
one disadvantage of this solution however is that you wont be able to distinguish between two different types of objects namely Employee and SecondTypes

Sign up to request clarification or add additional context in comments.

7 Comments

you can do it without dynamics...stackoverflow.com/questions/2962551/…
This could be possible, I think, if I used a JSONObject to hold my JSONArrays instead of a JSONArray to hold them, in the Java-code that creates (serializes) this JSON-string. But are you sure there isn't a solution made for JSONArrays with JSONArrays in them?
@jjchiw So, you're suggesting List<List<X>> ? In that case, what would X be?
@AskeBisgaard - there wont be any problem deserializing an array or arrays.. the problem is that the internal arrays are of different TYPES so a solution like this List<List<x>>() wont do because x isnt constant..
Your solution worked, but I'm still curious to see if it's possible to do this without giving each array property names. I'll give you the answer for now.
|
0

Use this online tool to create the C# classes for you. You can then fine tune the classes (Name, etc) as per you need. At least, you can get idea that the model classes that you are creating are correct or not.

JSON to C#

3 Comments

I tried it, just because I wanted to see if it could give me an idea of the design I was looking for - but it doesn't think the JSON-data is valid, which it is.
whichever library you might use.. an array of objects of different types are not valid c# objects. unless you switch to dynamics introduced in c# 4.0
@KapilKhandelwal Here it is if you want it: [[{"Initials":"ABV"},{"Initials":"NA"}],[],[{"Content":"Test","ToolId":17,"WrittenAtTime":"Sat Feb 25 06:10:16 CET 2012","Initials":"ABV"}]]
0

did you add [Serializable] attribute to your DataContainer?

1 Comment

No. I just tried it and it didn't make a difference. The dummy classes worked without it though, so I don't think it's necessary either.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.