Click here to Skip to main content
15,888,733 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have the method to Deserialize my json to Dictionary<string,object> but I am having trouble when my json is an object to turn that into Dictionary<string,object>.

I am getting an error on my second foreach loop
Error: cannot implicitly convert type 'system.collections.generic.dictionary<string, object=""> to 'system.collections.generic.keyvaluepair<string, object="">

//Here is the method

C#
public static Dictionary<string, object> DeserializeToDictionary(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, DeserializeToDictionary(myDictionary.Value.ToString()));
                   else if (myDictionary.Value is JArray)
                       copyOfResponseJson.Add(myDictionary.Key, DeserializeToDictionary(myDictionary.Value.ToString(), true));
                   else
                       copyOfResponseJson.Add(myDictionary.Key, myDictionary.Value);
               }
               return copyOfResponseJson;
           }
           else
           {
              var resposneJson1 = JsonConvert.DeserializeObject<List<Dictionary<string, object>>>(jsonString);
               var copyOfResponseJson2 = new Dictionary<string, object>();
               foreach(KeyValuePair<string, object> myDictionary in resposneJson1)
               {
                   if (myDictionary.Value is JObject)
                       copyOfResponseJson2.Add(myDictionary.Key, DeserializeToDictionary(myDictionary.Value.ToString()));
                   else if (myDictionary.Value is JArray)
                       copyOfResponseJson2.Add(myDictionary.Key, DeserializeToDictionary(myDictionary.Value.ToString(), true));
                   else
                       copyOfResponseJson2.Add(myDictionary.Key, myDictionary.Value);
               }
               return copyOfResponseJson2;

           }



//Updating the Question with one of the Json I am trying the Deserialize
{
	"groups": null,
	"data": [{
			"type": 123,
			"name": "Name123"
		},
		{
			"type": 567,
			"name": "SecondName"

		}
	],
	"total": 2

}


What I have tried:

I am not sure how to fix this error... Any help would be really great.
Posted
Updated 5-Aug-20 8:55am
v3
Comments
Garth J Lancaster 5-Aug-20 5:24am    
No sure exactly where you mean the error is coming from - can you perhaps use Improve question and add a comment in bold to show where the error occurs .. maybe also you could show us some of the Json or a mocked up version just so people can see what you're deserialising
Member 14779968 5-Aug-20 13:57pm    
Hello Garth thank you for your response.. I had updated the question and added the json I was working with..

Quote:
C#
var resposneJson1 = JsonConvert.DeserializeObject<List<Dictionary<string, object>>>(jsonString);
foreach (KeyValuePair<string, object> myDictionary in resposneJson1)
Your responseJson1 variable is a List<Dictionary<string, object>>.

Each item within the list is a Dictionary<string, object>.

You are trying to cast each item from the list to a KeyValuePair<string, object>, which cannot possibly work. There is no way to represent the entire dictionary as a single KeyValuePair.

How you fix it will depend on the source JSON you're trying to deserialize, which you haven't shown.
 
Share this answer
 
Comments
Member 14779968 5-Aug-20 14:01pm    
Hello Richard, thanks for your response.. I have updated question with one of the Json I was working with.. Please have a look.
Deserialize to a .NET class (e.g. Rootobject), and go from there. These are your classes:
C#
public class Rootobject {
   public object groups { get; set; }
   public Datum[] data { get; set; }
   public int total { get; set; }
}

public class Datum {
   public int type { get; set; }
   public string name { get; set; }
}
 
Share this answer
 
The problem is your foreach:
C#
var resposneJson1 = JsonConvert.DeserializeObject<List<Dictionary<string, object>>>(jsonString);
            var copyOfResponseJson2 = new Dictionary<string, object>();
            foreach (KeyValuePair<string, object> myDictionary in resposneJson1)

You are specifically saying that myDictionary is a KeyValuePair<string, object> but resposneJson1 is a List<Dictionary<string, object>> which means that each item the iterator returns is a Dictionary<string, object> rather than a KeyValuePair
Probably, you want to change it to
C#
foreach (Dictionary<string, object> myDictionary in resposneJson1)
But then you'll need to sort out the loop body to match.
 
Share this answer
 
Comments
Richard Deeming 5-Aug-20 5:59am    
Snap! :)

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