Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将对象转换为json字符串时忽略属性_Json_Wpf_Serialization_Properties_Odata - Fatal编程技术网

将对象转换为json字符串时忽略属性

将对象转换为json字符串时忽略属性,json,wpf,serialization,properties,odata,Json,Wpf,Serialization,Properties,Odata,所以我有一个RESTAPI,它与基于OData交换的JSON一起工作。在OData类型中,我要读取一个ID属性,以便对其进行检查。但是,当我想写回Web服务器时,ID属性不能出现在响应JSON字符串中。所以它必须是一个只写属性,但是简单地将属性更改为只写可以防止检查该属性的值 例如,我创建了一个新产品: Public Class Product Public property ID as integer Public property Title as string End class

所以我有一个RESTAPI,它与基于OData交换的JSON一起工作。在OData类型中,我要读取一个ID属性,以便对其进行检查。但是,当我想写回Web服务器时,ID属性不能出现在响应JSON字符串中。所以它必须是一个只写属性,但是简单地将属性更改为只写可以防止检查该属性的值

例如,我创建了一个新产品:

Public Class Product
  Public property ID as integer
  Public property Title as string
End class
获取响应:

{
  "ID" = 1,
  "Title" = "Cool product!"
}
发帖错误:

{
  "ID" = 1, <---- ignore this value
  "Title" = "Cool product! Changed!"
}
Web服务器使用OData

使用属性JsonIgnoredoesn无法修复它,因为REST响应的值当时没有序列化

这适用于WPF,而不是ASP.Net。JSON.Net支持使用返回bool的方法,该方法与带有ShouldSerialize前缀的属性同名:

public class Product
{
    public int Id { get; set; }
    public string Title { get; set; }

    public bool ShouldSerializeId() => false;

}

您正在使用Json.NET序列化产品吗?我正在使用Newtonsoft JSONTNX,明天将尝试!
public class Product
{
    public int Id { get; set; }
    public string Title { get; set; }

    public bool ShouldSerializeId() => false;

}