驼峰JSON的WebAPI子对象(嵌套对象,子对象) [英] CamelCase JSON WebAPI Sub-Objects (Nested objects, child objects)

查看:742
本文介绍了驼峰JSON的WebAPI子对象(嵌套对象,子对象)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建的儿童(嵌套对象)一个复杂的对象是从我的Web API控制器返回。对象包含其他对象类型的列表。在列表中,这些子对象类型按照.NET使用Pascal大小写。

I'm creating a complex object with children (nested objects) to be returned from my web api controller. The object contains list of other object types. These sub-object types within the list follow the pascal casing used in .NET.

var persons = peopleLookup.Values;
var users = userLookup.Values;
var roles = rolesLookup.Values;
var groups = groupLookup.Values;
var roleAssignments = roleAssignmentLookup.Values;
var groupMembers = groupMemberLookup.Values;
return new { persons, users, roles, roleAssignments, groups, groupMembers };

我的问题是的WebAPI不会每个子项的属性骆驼情况。比如第一人名单的人应该有和id属性,名称,而不是Id的.NET帕斯卡的情况下,名称。这同样适用于所有其他子项。

My problem is that WebAPI does not camel case each of the properties of the sub-items. For example the first person in the persons list should have and attributes of id, name instead of the .NET pascal case of Id, Name. The same should apply for all the other sub items.

推荐答案

您可以配置JSON.NET在应用程序启动时产生骆驼的名字。从code段<一个href=\"http://odeto$c$c.com/blogs/scott/archive/2013/03/25/asp-net-webapi-tip-3-camelcasing-json.aspx\">Scott阿伦的帖子:

You can configure JSON.NET to produce camel case names in your application startup. Code snippet from Scott Allen's post:

var formatters = GlobalConfiguration.Configuration.Formatters;
var jsonFormatter = formatters.JsonFormatter;
var settings = jsonFormatter.SerializerSettings;
settings.Formatting = Formatting.Indented;
settings.ContractResolver = new CamelCasePropertyNamesContractResolver();

这篇关于驼峰JSON的WebAPI子对象(嵌套对象,子对象)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆