Search code examples
c#jsonrestrestsharp

How to deserialize JSON without static key/property names


Hello I have problem with deserializing IRestResponse.Content JSON response.

{
"49": 
    {
        "9345": "2018-10-11", 
        "106": null, 
        "107": "4222238842", 
        "108": "CompanyName", 
        "8210": "2018-11-11/1", 
        "110": "00-300", 
        "109": "Street", 
        "112": "Country", 
        "18418": null, 
        "18420": "S\u0141ON", 
        "18422": "OtherString", 
        "9338": null, 
        "111": "City"
    }
}

I have tried some webpage's or built in VisualStudio converter but It gives my something like this.

public class Rootobject
{
    public _49 _49 { get; set; }
}

public class _49
{
    public string _9345 { get; set; }
    public object _106 { get; set; }
    public string _107 { get; set; }
    public string _108 { get; set; }
    public string _8210 { get; set; }
    public string _110 { get; set; }
    public string _109 { get; set; }
    public string _112 { get; set; }
    public object _18418 { get; set; }
    public string _18420 { get; set; }
    public string _18422 { get; set; }
    public object _9338 { get; set; }
    public string _111 { get; set; }
}

This look's ok but In my case those JSON files have dynamic property names and can have another "int" values. Also the nested content inside "49" can have less or more values. I am especially interested in gathering "49" << this value to variable.

I also have tried something like this, but doesn't work either:

public class DeserializeJsonContent
{
   public Dictionary<object, Dictionary<object, object>> values { get; set; }
}

Simplified code sample

    public List<T> JSONDeserialize<T>(IRestResponse response) where T : new()
    {
        var responseData = client.Deserialize<List<T>>(response);
        var ListDeserializedData = responseData.Data.ToList<T>();
        return ListDeserializedData;
    }

....

var response = rest.client.Execute(request);
if (response.IsSuccessful)
{
    var obj = rest.JSONDeserialize<DeserializeJsonContent>(response);
}

obj has count 1 but vals = null

Edit after Solve: I still have no idea why my deserialized class doesn't work in this case (I am using it in many other json deserialization response's) Thanks to xdtTransform answer I have tried and all of those worked

var obj2 = JsonConvert.DeserializeObject<Dictionary<string, Dictionary<object, object>>>(response.Content);
var obj3 = JsonConvert.DeserializeObject<Dictionary<string, Dictionary<string, string>>>(response.Content);
var obj4 = JsonConvert.DeserializeObject<Dictionary<int, Dictionary<string, string>>>(response.Content);
var obj5 = JsonConvert.DeserializeObject<Dictionary<object, Dictionary<string, string>>>(response.Content);
var obj6 = JsonConvert.DeserializeObject<Dictionary<object, Dictionary<object, object>>>(response.Content);
var obj7 = JsonConvert.DeserializeObject<Dictionary<object, Dictionary<int, object>>>(response.Content);

Then just

var value = obj2.First().Key;

Solution

  • Using Newtonsoft.Json, You can directly deserialize to Dictionary<string,Dictionary<string,string>> like :

    var obj = JsonConvert.DeserializeObject<Dictionary<string,Dictionary<string,string>>>(input);
    

    If you need it to be in your custom type, you should declare this type like :

    public class WrapperType : Dictionary<string,Dictionary<string,string>>{}
    

    Then the Deserialisation stay the same:

     var obj = JsonConvert.DeserializeObject<WrapperType>(input);
    

    Updated demo