Convert from Object to Dictionary.

I need to convert from Object to dictionary. I tried in the following way

public void Receivedfriends(Object obj)
	{
 		Dictionary<string,object> ht = new Dictionary<string,object>();
		ht = obj as Dictionary<string,object>;
		
		if(ht.ContainsKey ("data"))
		{	
			List<string> ArrList = new List<string>();
			ArrList= ht["data"];
 	  		
 	  		CONTROLLER.FriendsID = new string[ArrList.Count];
 	  		CONTROLLER.FriendsName = new string[ArrList.Count];
 	  			
 			Dictionary<string,object> htt = new Dictionary<string,object>();
 			string idstr = "";
 			string namestr = "";

. . . 
. . . ..

But I got error like
error CS0266: Cannot implicitly convert type object' to System.Collections.Generic.List’.

How could i solve this???

What kind of data would you normally be passing into this function?

I assume the error occurs on line:

ht = obj as Dictionary<string,object>;

Which is trying to explicitly cast a generic object type into a more complex List type.

You could try:

ht = obj as List<string,object>;

or

ht = (List<string,object>)obj;

because type Dictionary inherits from List.

Yeah that’s a bit of a mess :slight_smile:

Firstly you don’t need to new things that you then throw away!

So you’ve got the first cast, but you aren’t casting ht[“data”] so it’s still object.

public void Receivedfriends(Object obj)
    {
       var ht = obj as Dictionary<string,object>;
 
       if(ht.ContainsKey ("data"))
       { 
         var ArrList= ht["data"] as List<string>;
 
             CONTROLLER.FriendsID = new string[ArrList.Count];
             CONTROLLER.FriendsName = new string[ArrList.Count];
 
       Dictionary<string,object> htt = new Dictionary<string,object>();
       string idstr = "";
       string namestr = "";