0

Currently, I have some JSON data that I am attempting to deserialize using the DataContractJsonSerializer class. However, one of the arrays in the data contains multiple types of objects. Is there a way to deserialize this data properly? I am aware that a very similar question exists, but I would prefer not to use Json.NET or any other third-party libraries.

EDIT: A small example:

In this instance, let's say the JSON is of form [{"foo":string},{"bar":string},{"foo":string},{"foo":string},...] where each element is either of form {"foo":string} or {"bar":string}. Then, the contracts could be set up as such:

[DataContract]
class Foo { [DataMember] public string foo; }

[DataContract]
class Bar { [DataMember] public string bar; }

In this context, my question is, how do I deserialize this array of Foos and Bars?

2
  • However, one of the arrays in the data contains multiple types of objects -- could you provide an example? How are these object types different? If for some reason they are completely different you may consider dynamic type. Posting a sample of the JSON and the model you are attempting to deserialize it to would be helpful. Commented Feb 22, 2017 at 16:20
  • @ShaneRay Added a small example, in the real case, my Foo and Bar have completely different properties. Commented Feb 22, 2017 at 16:26

1 Answer 1

0

This does not sound right. There should not be two completely different types in a single array. Given the JSON provided I would try something like this....

[DataContract]
class SomeClass
{
    [DataMember]
    public string foo { get; set;}
    [DataMember]
    public string bar { get; set;}
}

Then check for IsNullOrWhiteSpace() on each property.

Updated with more code...

   static void Main(string[] args)
    {
        SomeClass[] output;
        var json = "[{\"foo\":\"value\"},{\"bar\":\"value\"},{\"foo\":\"value1\"},{\"foo\":\"value1\"}]";

        using (var ms = new MemoryStream(Encoding.Unicode.GetBytes(json)))
        {
            var deserializer = new DataContractJsonSerializer(typeof(SomeClass[]));
             output = (SomeClass[])deserializer.ReadObject(ms);
        }

        // do something with output
        Console.WriteLine(output.Length);
    }
Sign up to request clarification or add additional context in comments.

8 Comments

Specifically, I am querying a certain JSON web API which gives 2 responses (with different formats) in an array.
In that case you need to have two separate models. For the first API call that returns an array you deserialize to ModelA. The second API call you deserialize to ModelB.
I think you're misunderstanding what I'm trying to say. I pass one request to the server, and receive a single response with an array with a Foo and a Bar. I'm asking here how to deserialize this array to retrieve the Foo and Bar in it.
The JSON you provided is an array of objects... It sounds like you may be referring to a dictionary but foo is repeated so it can not be an indexer. I have updated my answer with more example code.
I will, just waiting a day or two to make sure no better answers crop up.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.