C# .NET JavaScriptSerializer - Deserializing Response that contains a String or a String Array?

238 views Asked by At

I have an SSIS C# script component that hits an API, pulls down the response, and I'm working on deserializing it into a proper C# object.

Here's the basic JSON that the API returns:

{
    "id":1234,
    "answers": [
        "answer one"
    ]
}

Here's the corresponding C# class:

internal class ApiResponse {
     public string id { get; set; }
     public string[] answers { get; set; }
}

Then from using System.Web.Script.Serialization I declare a JavaScriptSerializer and use it to deserialize the response:

JavaScriptSerializer js = new JavaScriptSerializer();
ApiResponse apiResponse = js.Deserialize<ApiResponse>(rawJsonFromApi);

This works perfectly for most inputs, but in some cases, the "answers" array is empty, and the API returns a string that confirms that:

{
    "id":2345,
    "answers": "The user did not provide any answers."
}

What can I do to make the JavaScriptSerializer expect to see either an array of strings or just a string so that it can process both JSON objects with one method/set of code?

0

There are 0 answers