Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Dear All,
i have variable List Dictionary<string,string> which contain list of object as key value pairs (dictionary items as object attributes )
ex _ListDataDict[0]={{"lstName","pla"},{"Age"},"20"} how can i casting data-dictionary list to list of object Person
List<Dictionary<string, string>> _ListDataDict

 //class Attributes
class person {
string lstName;
string Age;
}
Posted
Updated 22-Apr-15 2:54am
v4
Comments
Sinisa Hajnal 22-Apr-15 9:10am    
You can't. You can go through the list and assign values...or you could make a list<Person> and not use dictionary as list type in the first place.

If you have to have start with dictionary, you could use new List<keyvaluepair<string, string="">> (_listdatadict) and get the list of pairs and then do some Linq magic to get it to person list...
Alexander Dymshyts 22-Apr-15 9:18am    
Why don't you use List<person>?
Member 11593359 22-Apr-15 9:34am    
itried to use

but it give me an error Cannot implicitly convert type 'object' to 'Person'. An explicit conversion exists (are you missing a cast?)

<pre lang="c#">[HttpPost]
public HttpResponseMessage Post(List<dictionary<string, string=""> _ListDataDict)
{

if (_ListDataDict != null)
{
foreach (var kvp in _ListDataDict)
{
Person p =JsonConvert.DeserializeObject(kvp.ToString());
}
return ControllerContext.Request.CreateResponse(HttpStatusCode.OK, "The Messages Has Been Successfully Sent Via Sending data Dictionary");
}
else
return ControllerContext.Request.CreateResponse(HttpStatusCode.InternalServerError, "SMS Object = NULL Value");
}</pre>
any idea why

1 solution

You cannot cast a Dictionary into an object of any type directly.
This is what you will need to do
Person p = JsonConvert.DeserializeObject<person>(_ListDataDict[0]);</person>

Json.Convert is available in Newtonsoft.Json dll which can be downloaded from the web.
 
Share this answer
 
Comments
Member 11593359 22-Apr-15 9:20am    
itried to use

but it give me an error Cannot implicitly convert type 'object' to 'Person'. An explicit conversion exists (are you missing a cast?)

<pre lang="c#">[HttpPost]
public HttpResponseMessage Post(List<Dictionary<string, string> _ListDataDict)
{

if (_ListDataDict != null)
{
foreach (var kvp in _ListDataDict)
{
Person p =JsonConvert.DeserializeObject(kvp.ToString());
}
return ControllerContext.Request.CreateResponse(HttpStatusCode.OK, "The Messages Has Been Successfully Sent Via Sending data Dictionary");
}
else
return ControllerContext.Request.CreateResponse(HttpStatusCode.InternalServerError, "SMS Object = NULL Value");
}</pre>
any idea why
Richard Deeming 22-Apr-15 9:52am    
Try:

string json = JsonConvert.SerializeObject(kvp);
Person p = JsonConvert.DeserializeObject<Person>(json);
Member 11593359 22-Apr-15 10:03am    
thank you so much it is working now

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