Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
//Here is the Json
{
    "groups": null,
    "data": [{
            "type": 123,
            "name": "Name123"
        },
        {
            "type": 567,
            "name": "SecondName"

        }
    ],
    "total": 2

}




// I am then deserializing my json to either dictionary or list.. in this case it would be a List.. and then I am using a foreach loop to first get the Type..
result[t].GetType() gives me the exception

var result = Helpers.JsonHelper.DeserializeToDictionaryOrList(jsonString);

    foreach (var t in result.Keys)
                    {
                        var actualType = result[t].GetType();  

                    }



//Here the DeserializeToDictionaryOrList method for reference
public static class JsonHelper
   {
       public static dynamic DeserializeToDictionaryOrList(string jsonString, bool isArray = false)
       {
           if (!isArray)
               isArray = jsonString.Substring(0, 1) == "[";

           if (!isArray)
           {
               var responseJson = JsonConvert.DeserializeObject<Dictionary<string, object>>(jsonString);
               var copyOfResponseJson = new Dictionary<string, object>();
               foreach (KeyValuePair<string, object> myDictionary in responseJson)
               {
                   if (myDictionary.Value is JObject)
                       copyOfResponseJson.Add(myDictionary.Key, DeserializeToDictionaryOrList(myDictionary.Value.ToString()));
                   else if (myDictionary.Value is JArray)
                       copyOfResponseJson.Add(myDictionary.Key, DeserializeToDictionaryOrList(myDictionary.Value.ToString(), true));
                   else
                       copyOfResponseJson.Add(myDictionary.Key, myDictionary.Value);
               }
               return copyOfResponseJson;
           }
           else
           {
               var responseJson = JsonConvert.DeserializeObject<List<object>>(jsonString);
               var copyOfResponseJson = new List<object>();
               foreach (var myDictionary in responseJson)
               {
                   if (myDictionary is JObject)
                       copyOfResponseJson.Add(DeserializeToDictionaryOrList(myDictionary.ToString()));
                   else if (myDictionary is JArray)
                       copyOfResponseJson.Add(DeserializeToDictionaryOrList(myDictionary.ToString(), true));
                   else
                       copyOfResponseJson.Add(myDictionary);
               }
               return copyOfResponseJson;
           }
       }


What I have tried:

I am not sure why this is causing the exception.. it was just fine with another Json.. I am guessing because first time result[t] is groups and the value in the json is null.. I am not really sure about this..
Posted
Updated 5-Aug-20 19:53pm
v2
Comments
Sandeep Mewara 6-Aug-20 1:47am    
When you deserialize, shoudn't you tell the Type it should be of? Given that might be happening in your method DeserializeToDictionaryOrList, whats the type of result here?
I believe the error would be on foreach loop. If so, can you update the question with information about method Helpers.JsonHelper.DeserializeToDictionaryOrList
Member 14779968 6-Aug-20 1:51am    
Hi Sandeep sure. I have updated the question and included the DeserializeToDictionaryOrList method
Sandeep Mewara 6-Aug-20 2:00am    
Based on the type you expect 'Dictionary' it seems you might be expecting it on 'data' in json. not entire json - any reason on why convert all as dictionary?

Did you validate the value of copyOfResponseJson during DEBUG to see it holds expected values?
Member 14779968 6-Aug-20 2:17am    
Yes I did. I get all the keys and values in copyOfResponseJson. I get this..
[0] {[groups, null]}
[1] {[data, Count = 100]}
[2] {[total, 110]}
Sandeep Mewara 6-Aug-20 2:12am    
BTW, I don't know the intent, if the schema of json is known, I would deseriealize into class and then make use of it's object for any usage.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900