Using invalid C # keyword name when exporting to JSON

I am using a third party api that expects JSON with keyword names like "key-name".

Using Entity Framework I am doing the following

var result = _context.data.Select(d => new 
{
    keyName = x.name
});
return Json(new {result = result});

      

Is there a way to use the corresponding value without replacing strings after manual generation?

+3


source to share


1 answer


You can create a new class for the json result.
eg:



public class JsonResult{

[JsonProperty(Name="key-name")]
   public string KeyName{get;set;}
}

var result = _context.data.Select(d => new JsonResult
{
    KeyName = x.name
});
return Json(new {result = result});

      

+2


source







All Articles