Michael Hartmann
Michael Hartmann

Reputation: 584

How to deserialize a JSON array containing different data types to a single object

Over the last few days I have been researching on how to deserialize a JSON response to and C# object. The JSON is valid but I can not get any JSON to C# converters to convert it. I also cannot find an answer on Stackoverflow that works for this instance. The JSON Response is:

[
  {
    "SEX": "Male",
    "BREED": "Opifex",
    "PVPRATING": 1301,
    "NAME": "Kilmanagh",
    "FIRSTNAME": "Big",
    "PVPTITLE": "Freshman",
    "LASTNAME": "Kahuna",
    "CHAR_DIMENSION": 5,
    "ALIENLEVEL": 30,
    "RANK_name": "Vindicator",
    "HEADID": 40281,
    "PROFNAME": "Guru",
    "LEVELX": 220,
    "PROF": "Martial Artist",
    "CHAR_INSTANCE": 12734,
    "SIDE": "Omni"
  },
  {
    "ORG_DIMENSION": 5,
    "RANK_TITLE": "President",
    "ORG_INSTANCE": 9911,
    "NAME": "Elements of Destruction",
    "RANK": 0
  },
  "2016/04/06 08:37:26"
]

From my inspection it is an array that contains two objects and a string. I have used the following to attempt to convert it to an object:

 resultArray = JsonConvert.DeserializeObject<List<JsonWhoisResult>>(data);
 and
 result = JsonConvert.DeserializeObject<JsonWhoisResult>(data);

Either way I get an error:

Error converting value ...(snip)... [ConsoleApplication6.JsonWhoisResult]'. Path '', line 1, position 536.`

I do not know if I have the object wrong, or if I am using incorrect code for this JSON format. I am using:

public class JsonWhoisResult
{
    public stats stats { get; set; }
    public header header { get; set; }
    public string datetime { get; set; }
}

public class header
{
    public int ORG_DIMENSION { get; set; }
    public string RANK_TITLE { get; set; }
    public int ORG_INSTANCE { get; set; }
    public string NAME { get; set; }
    public int RANK { get; set; }
}

public class stats
{
    public string SEX { get; set; }
    public string BREED { get; set; }
    public int PVPRATING { get; set; }
    public string NAME { get; set; }
    public string FIRSTNAME { get; set; }
    public string PVPTITLE { get; set; }
    public string LASTNAME { get; set; }
    public int CHAR_DIMENSION { get; set; }
    public int ALIENLEVEL { get; set; }
    public string RANK_name { get; set; }
    public int HEADID { get; set; }
    public string PROFNAME { get; set; }
    public int LEVELX { get; set; }
    public string PROF { get; set; }
    public int CHAR_INSTANCE { get; set; }
    public string SIDE { get; set; }
}

If anyone has any solutions I would really appreciate it. I have several more that use this type of style. If I can get a solution then I should be able to apply it to the rest.

Upvotes: 1

Views: 4532

Answers (2)

Richard Irons
Richard Irons

Reputation: 1473

You can use JSON.net's support for dynamic to do this. I just tested this with the JSON as you pasted it above, and it works - but note, this is not strongly typed.

dynamic result = JsonConvert.DeserializeObject(json);

// value is "Freshman"
Console.WriteLine("result[0].PVPTITLE = '{0}'", result[0].PVPTITLE);

// value is "President"
Console.WriteLine("result[1].RANK_TITLE = '{0}'", result[1].RANK_TITLE);

// value is 2016-04-06 08:37:27
Console.WriteLine("result[2] '{0}'", (DateTime)result[2]);

Upvotes: 0

Brian Rogers
Brian Rogers

Reputation: 129707

Because your JSON is an array you cannot deserialize into a single JsonWhoisResult, and because your array contains disparate object types, you cannot deserialize directly into a List<JsonWhoisResult>. You will need to make a custom JsonConverter to handle this situation. The converter can use Json.Net's LINQ-to-JSON API to deserialize the JSON, then manually extract each item into its appropriate object type and populate a single JsonWhoisResult as you want. Something like this should work:

class JsonWhoisResultConverter : JsonConverter
{
    public override bool CanConvert(Type objectType)
    {
        return (objectType == typeof(JsonWhoisResult));
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        JArray array = JArray.Load(reader);
        JsonWhoisResult result = new JsonWhoisResult();
        result.stats = array[0].ToObject<stats>();
        result.header = array[1].ToObject<header>();
        result.datetime = array[2].ToString();
        return result;
    }

    public override bool CanWrite
    {
        get { return false; }
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        throw new NotImplementedException();
    }
}

Then use it like this:

JsonWhoisResult result = JsonConvert.DeserializeObject<JsonWhoisResult>(json, new JsonWhoisResultConverter());

Fiddle: https://dotnetfiddle.net/d1hkCn

Upvotes: 4

Related Questions