Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using an invalid C# keyword name when exporting to JSON

Tags:

json

c#

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

Using Entity Framework I do the following

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

Is there a way to use the appropriate value without replacing the strings after generation manually?

like image 471
kechap Avatar asked Jan 30 '26 14:01

kechap


1 Answers

You can create a new class for json result.
e.g:

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});
like image 136
James.YinG Avatar answered Feb 02 '26 02:02

James.YinG



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!