Para ter uma maior liberdade na customização do JSON que será retornado eu recomendo que você utilize uma biblioteca chamada Json.NET (Link do pacote no Nuget).
Depois de instalar o Json.NET no projeto defina um model que será "serializado" para JSON pela biblioteca e utilize o atributo JsonProperty
para definir um nome customizado para cada propriedade do JSON que será retornado.
Montei dois models com base no JSON de exemplo na sua pergunta:
public class Thing
{
[JsonProperty(PropertyName = "title")]
public string Title { get; set; }
[JsonProperty(PropertyName = "description")]
public string Description { get; set; }
[JsonProperty(PropertyName = "thumbnail")]
public string[] Thumbnail { get; set; }
[JsonProperty(PropertyName = "large")]
public string[] Large { get; set; }
[JsonProperty(PropertyName = "button_list")]
public ThingButton[] ButtonList { get; set; }
[JsonProperty(PropertyName = "tags")]
public string[] Tags { get; set; }
}
public class ThingButton
{
[JsonProperty(PropertyName = "title")]
public string Title { get; set; }
[JsonProperty(PropertyName = "url")]
public string Url { get; set; }
}
Será necessário também extender JsonResult
para suportar a biblioteca Json.NET, para fazer isso utilizei um classe criada por um usuário do SOen nessa resposta aqui, ela está bem feita e pode ser adicionada ao seu projeto sem nenhum problema:
public class JsonNetResult : JsonResult
{
public override void ExecuteResult(ControllerContext context)
{
if (context == null)
throw new ArgumentNullException("context");
var response = context.HttpContext.Response;
response.ContentType = !String.IsNullOrEmpty(ContentType)
? ContentType
: "application/json";
if (ContentEncoding != null)
response.ContentEncoding = ContentEncoding;
// If you need special handling, you can call another form of SerializeObject below
var serializedObject = JsonConvert.SerializeObject(Data, Formatting.Indented);
response.Write(serializedObject);
}
}
E então montei um action method para exemplificar a utilização:
public class HomeController : Controller
{
public JsonNetResult GetThing()
{
var thing = new Thing
{
Title = "Gallery 1",
Description = "Description of gallery...",
Thumbnail = new string[]
{
"1.jpg",
"2.jpg",
"3.jpg",
"4.jpg",
"11.jpg"
},
Large = new string[]
{
"large/1.jpg",
"large/2.jpg",
"large/3.jpg",
"large/4.jpg",
"large/11.jpg"
},
ButtonList = new ThingButton[]
{
new ThingButton { Title = "Demo", Url = "http://demo.com" },
new ThingButton { Title = "Download", Url = "http://download.com" }
},
Tags = new string[]
{
"All",
"Photoshop"
}
};
return new JsonNetResult { Data = thing, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
}
}
E esse é o retorno de uma requisição GET ao endpoint /Home/GetThing
:
{
"title": "Gallery 1",
"description": "Description of gallery...",
"thumbnail": [
"1.jpg",
"2.jpg",
"3.jpg",
"4.jpg",
"11.jpg"
],
"large": [
"large/1.jpg",
"large/2.jpg",
"large/3.jpg",
"large/4.jpg",
"large/11.jpg"
],
"button_list": [
{
"title": "Demo",
"url": "http://demo.com"
},
{
"title": "Download",
"url": "http://download.com"
}
],
"tags": [
"All",
"Photoshop"
]
}