JSON

【ASP.NET Web API教程】6.2 ASP.NET Web API中的JSON和XML序列(2)

字号+ 作者:H5之家 来源:H5之家 2015-10-21 13:24 我要评论( )

// Convert all dates to UTC// 将所有日期转换成UTC格式var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter;json.SerializerSettings.DateTimeZoneHandling =Newtonsoft.Json.DateTimeZoneHa

// Convert all dates to UTC // 将所有日期转换成UTC格式 var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter; json.SerializerSettings.DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Utc;

If you prefer to use Microsoft JSON date format ("\/Date(ticks)\/") instead of ISO 8601, set the DateFormatHandling property on the serializer settings:
如果你喜欢使用微软的JSON日期格式("\/Date(ticks)\/ ")而不是ISO 8601,可以在SerializerSettings上设置DateFormatHandling属性:

var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter; json.SerializerSettings.DateFormatHandling = Newtonsoft.Json.DateFormatHandling.MicrosoftDateFormat;

Indenting(缩进)

To write indented JSON, set the Formatting setting to Formatting.Indented:
为了书写有缩进的JSON,可以将Formatting设置为Formatting.Indented:

var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter; json.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented;

Camel Casing(驼峰式大小写转换)

To write JSON property names with camel casing, without changing your data model, set the CamelCasePropertyNamesContractResolver on the serializer:
为了在不修改数据模型的情况下,用驼峰式大小写转换JSON的属性名,可以设置序列化器上的CamelCasePropertyNamesContractResolver:

var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter; json.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();

Anonymous and Weakly-Typed Objects
匿名类型与弱类型对象

An action method can return an anonymous object and serialize it to JSON. For example:
动作方法或以返回一个匿名对象,并将其序列化成JSON。例如:

public object Get() { return new { Name = "Alice", Age = 23, Pets = new List<string> { "Fido", "Polly", "Spot" } }; }

The response message body will contain the following JSON:
响应消息体将含有以下JSON:

{"Name":"Alice","Age":23,"Pets":["Fido","Polly","Spot"]}

If your web API receives loosely structured JSON objects from clients, you can deserialize the request body to a Newtonsoft.Json.Linq.JObject type.
如果Web API从客户端接收了松散结构的JSON,你可以将该请求体解序列化成Newtonsoft.Json.Linq.JObject类型。

public void Post(JObject person) { string name = person["Name"].ToString(); int age = person["Age"].ToObject<int>(); }

However, it is usually better to use strongly typed data objects. Then you don't need to parse the data yourself, and you get the benefits of model validation.
然而,通常更好的是使用强类型数据对象。那么,便不需要自行对数据进行解析,并且能得到模型验证的好处。

The XML serializer does not support anonymous types or JObject instances. If you use these features for your JSON data, you should remove the XML formatter from the pipeline, as described later in this article.
XML序列化器不支持匿名类型或JObject实例。如果将这些特性用于JSON数据,应该去掉管线中的XML格式化器,如本文稍后描述的那样。

6.2.3 XML Media-Type Formatter
6.2.3 XML媒体类型格式化器

XML formatting is provided by the XmlMediaTypeFormatter class. By default, XmlMediaTypeFormatter uses the DataContractSerializer class to perform serialization.
XML格式化是由XmlMediaTypeFormatter类提供的。默认情况下,XmlMediaTypeFormatter使用DataContractSerializer类来执行序列化。

If you prefer, you can configure the XmlMediaTypeFormatter to use the XmlSerializer instead of the DataContractSerializer. To do so, set the UseXmlSerializer property to true:
如果喜欢,你可以将XmlMediaTypeFormatter配置成使用XmlSerializer而不是DataContractSerializer。要想这么做,可将UseXmlSerializer属性设置为true:

var xml = GlobalConfiguration.Configuration.Formatters.XmlFormatter; xml.UseXmlSerializer = true;

The XmlSerializer class supports a narrower set of types than DataContractSerializer, but gives more control over the resulting XML. Consider using XmlSerializer if you need to match an existing XML schema.
XmlSerializer类支持的类型集要比DataContractSerializer更窄一些,但对结果XML有更多的控制。如果需要与已有的XML方案匹配,可考虑使用XmlSerializer。

6.2.4 XML Serialization
6.2.4 XML序列化

This section describes some specific behaviors of the XML formatter, using the default DataContractSerializer.
本小节描述使用默认DataContractSerializer的时,XML格式化器的一些特殊行为。

By default, the DataContractSerializer behaves as follows:
默认情况下,DataContractSerializer行为如下:

  • All public read/write properties and fields are serialized. To omit a property or field, decorate it with the IgnoreDataMember attribute.
    序列化所有public读/写属性和字段。为了忽略一个属性或字段,请用IgnoreDataMember注解属性修饰它。
  • Private and protected members are not serialized.
    private和protected成员不作序列。
  • Read-only properties are not serialized.
    只读属性不作序列化
  • Class and member names are written in the XML exactly as they appear in the class declaration.
    类名和成员名按类声明中的确切呈现写入XML
  • A default XML namespace is used.
    使用XML的默认命名空间
  •  

    1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

    相关文章
    • php CI 实战教程:[5]用curl获取json并解析

      php CI 实战教程:[5]用curl获取json并解析

      2016-02-26 17:00

    • ASP.NET Web API 2 返回 Json格式

      ASP.NET Web API 2 返回 Json格式

      2016-02-10 17:18

    • Android中JSON数据格式读取解析创建视频教程

      Android中JSON数据格式读取解析创建视频教程

      2016-02-05 19:00

    • HttpClient 4.3教程 第一章 基本概念

      HttpClient 4.3教程 第一章 基本概念

      2016-01-26 16:49

    网友点评