简体   繁体   English

Json.Net序列化/反序列化类名属性C#

[英]Json.Net serialize/deserialize Class Name attribute C#

Sorry for the (maybe) trivial question but, I'm trying to consume a web service where the entities and my data model classes are named different. 对于(也许)微不足道的问题,我很抱歉,我正在尝试使用一种Web服务,其中实体和我的数据模型类被命名为不同。

I want to keep my model .Net Class name and use a Json Attribute name to map, serializer/deserializer, with the corresponding web service entity. 我想保留我的模型.Net类名,并使用Json属性名称来映射,序列化器/反序列化器,以及相应的Web服务实体。 For example: 例如:

Web Service Entity: Web服务实体:

"People" “人”

My Model Class: 我的模型类:

"Employee" “雇员”

What I've already do: 我已经做了什么:

[JsonObject(Title="People")]
public class Employee 
{

   [JsonProperty("DifferentPropertyName")]
   string propertyName1 { get; set; }
}

But the json serializer/Deserializer continues to use the .Net class name and I need to set the jsonObject Title. 但是json序列化器/反序列化器继续使用.Net类名,我需要设置jsonObject标题。

There is a way to achieve it? 有办法实现吗?

EDIT 编辑

I'm working on a Xamarin Forms app, using Simple.OData.Client to consume an OData Service 我正在使用Xamarin Forms应用程序,使用Simple.OData.Client来使用OData服务

Thanks 谢谢

DataContractAttribute may be your solution. DataContractAttribute可能是您的解决方案。

public class RichFilter
{
   public Trolo item { get; set; }
}
[DataContract(Name = "item")]
public class Trolo 
{ 
   public string connector { get; set; } 
}

If you serialize a RichFilter object, here is the output : 如果序列化RichFilter对象,则输出如下:

{"item":{"connector":"AND"}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM