I Love Stackoverflow
I Love Stackoverflow

Reputation: 6868

How to create response from dictionary?

I am trying to create below response from dictionary:

['Employee1'] : List of skills

Code :

public class Skills
{
   public string Skill {get;set;}
}
var skills=FetchSkills(); 
var dictionary = new Dictionary<string, List<Skills>>();
dictionary.Add('Employee1',skills);

Now i am trying to create below response:

'Employee1' =
 {
    {"skill":"skill1"},{"skill":"skill2"},{"skill":"skill3"}
 }

I want skill property in camel case in my final response.

This is how i am trying to create response but not getting how to create expected response:

return Json(dictionary.Select
(

), JsonRequestBehavior.AllowGet);

Upvotes: 1

Views: 636

Answers (1)

Nkosi
Nkosi

Reputation: 247223

Given class

public class Skills {
   [JsonProperty("skill")]
   public string Skill {get;set;}
}

and used like this

var skills=FetchSkills(); 
var dictionary = new Dictionary<string, List<Skills>>();
dictionary.Add('Employee1',skills);

return Json(dictionary, JsonRequestBehavior.AllowGet);

should produce

{
   "Employee1":[
       {"skill":"skill1"},{"skill":"skill2"},{"skill":"skill3"}
   ]
}

Upvotes: 1

Related Questions