我的课程设置如下:
public class Foo
{
public string string1 { get; set; }
public string string2 { get; set; }
public string string3 { get; set; }
}
我正在使用Json.Net反序列化以下Json响应:
string json = "[{\"number1\": 1, \"number2\": 12345678901234567890, \"number3\": 3},
{\"number1\": 9, \"number2\": 12345678901234567890, \"number3\": 8}]";
反序列化代码:
List<Foo> foos = JsonConvert.DeserializeObject<List<Foo>>(json);
第二个数字超过int-64,但我并不真正关心检索该值。有没有办法将'number2'属性强制转换为字符串,或者在反序列化期间完全忽略它?
我尝试将'[JsonConverter(typeof(string))]属性添加到string2属性,但收到错误:'创建System.String时出错'。我也尝试过设置typeof(十进制)。
我也尝试使用[JsonIgnore],但这不起作用。
答案 0 :(得分:31)
您可以使用MissingMemberHandling
对象的JsonSerializerSettings
属性。
使用示例:
var jsonSerializerSettings = new JsonSerializerSettings();
jsonSerializerSettings.MissingMemberHandling = MissingMemberHandling.Ignore;
JsonConvert.DeserializeObject<YourClass>(jsonResponse, jsonSerializerSettings);
更多信息here。
答案 1 :(得分:11)
这是一个蹩脚的解决方法,但您可以创建一个方法来手动加载json。如果在没有自动反序列化器的情况下加载太多数据,只需删除您不想要的节点。这虽然慢得多。
public static List<Foo> FromJson(string input) {
var json = JToken.Parse(input);
json["key"].Remove();
var foo = JsonConvert.DeserializeObject<List<Foo>>(json.ToString());
}
这是一个有趣的问题,我想知道是否有人有更好的解决方案。
答案 2 :(得分:7)
以下是Newtonsoft Json忽略属性的首选方法,无需根据http://james.newtonking.com/json/help/index.html?topic=html/ReducingSerializedJSONSize.htm
修改类这个用于忽略EF或Linq2Sql的惰性引用属性
public class DynamicContractResolver : DefaultContractResolver
{
protected override IList<JsonProperty> CreateProperties(Type type,
MemberSerialization memberSerialization)
{
Func<Type,bool> includeProperty = t => t.IsValueType || t.Namespace.StartsWith("System") && t.Namespace.StartsWith("System.Data")==false;
IList<JsonProperty> properties = base.CreateProperties(type, memberSerialization);
var allProperties = properties.Select (p => new{p.PropertyName,Including=includeProperty(p.PropertyType), p.PropertyType});//.Dump("props");
var warnProperties=allProperties.Where (a =>a.Including && a.PropertyType.IsValueType==false && a.PropertyType.Name.IsIgnoreCaseMatch("String")==false) ;
//linq pad debugging helper
//var propertyTypesSerializing= allProperties.Where (p => p.Including).Select (p => p.PropertyType).Distinct().OrderBy (p => p.Name).Dump();
if(warnProperties.Any())
{
//LinqPad helper
//Util.Highlight(warnProperties.ToArray()).Dump("warning flag raised, aborting");
throw new ArgumentOutOfRangeException();
}
properties = properties.Where(p =>includeProperty(p.PropertyType)).ToList();
return properties;
}
}
所有.Dump()
调用只是linqpad调试助手,不需要方法调用。
样本用法:
var inactives = from am in Aspnet_Memberships
join mm in Member_members on am.UserId equals mm.Member_guid
where mm.Is_active==false && mm.Org_id==1
select new{am,mm};
//inactives.Take(4).ToArray().Dump();
var serialized = JsonConvert.SerializeObject(
inactives.Skip(1).Select(i => i.mm).First(),
new JsonSerializerSettings()
{
ContractResolver = new DynamicContractResolver(),
PreserveReferencesHandling = PreserveReferencesHandling.None,
ReferenceLoopHandling= ReferenceLoopHandling.Ignore
});
//.Dump();
答案 3 :(得分:3)
与@Maslow's solution类似,您可以使用another general purpose "ignorer":
var jsonResolver = new IgnorableSerializerContractResolver();
// ignore your specific property
jsonResolver.Ignore(typeof(Foo), "string2");
// ignore single datatype
jsonResolver.Ignore(typeof(System.Data.Objects.DataClasses.EntityObject));
var jsonSettings = new JsonSerializerSettings() { ReferenceLoopHandling = ReferenceLoopHandling.Ignore, ContractResolver = jsonResolver };
答案 4 :(得分:1)
替代;
如果ResponseAttribute具有模型或字符串参数
public class ResponseAttribute : Attribute { }
public class ModelItem
{
[Response]
public Guid Id { get; set; }
}
代码;
public class CustomJsonSerializer : JsonSerializerSettings
{
public CustomJsonSerializer()
{
ContractResolver = new CustomContractResolver();
}
public CustomJsonSerializer(params string[] members)
{
ContractResolver = new CustomContractResolver(members);
}
public class CustomContractResolver : DefaultContractResolver
{
public string[] Members { get; set; }
public CustomContractResolver(params string[] _members)
{
Members = _members;
}
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
{
JsonProperty property = base.CreateProperty(member, memberSerialization);
if (Members?.Length > 0)
property.ShouldSerialize = instance => { return Members.Contains(member.Name); };
else
property.ShouldSerialize = instance => { return member.GetCustomAttribute<ResponseAttribute>() != null; };
return property;
}
}
}
使用;
return new JsonResult(model, new CustomJsonSerializer());
或
return new JsonResult(model, new CustomJsonSerializer("Id","Test","Test2"));
答案 5 :(得分:0)
添加到drzaus答案:
您可以使用他建议的DefaultContractResolver
..只需在其CreateProperty
中使用property.Ignored = true;
而不是property.ShouldSerialize
,那么当您将JsonSerializerSettings
传递给DeserializeObject
函数或SerializeObject
函数。
答案 6 :(得分:0)
我遇到了类似的问题,但我的类包含 List<> 和 Dictionary<>,它们已填充,不应被 JSON 文件所具有的任何内容覆盖。对我来说,将数据加载到临时对象中然后只提取我需要的项目比当时我能找到的任何其他方法更容易。
所以对于这个例子,像这样......
public class Foo
{
public string string1 { get; set; }
public string string2 { get; set; }
public string string3 { get; set; }
}
List<Foo> foos = new List<Foo>();
List<Foo> tmp= JsonConvert.DeserializeObject<List<Foo>>(json);
foreach(Foo item in tmp)
{
foos.string1 = tmp.string1;
foos.string3 = tmp.string3;
}
答案 7 :(得分:0)
这段代码对我来说就像一个魅力:
using System.Reflection;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
public class PropertyRenameAndIgnoreSerializerContractResolver : DefaultContractResolver
{
private readonly Dictionary<Type, HashSet<string>> _ignores;
private readonly Dictionary<Type, Dictionary<string, string>> _renames;
public PropertyRenameAndIgnoreSerializerContractResolver()
{
_ignores = new Dictionary<Type, HashSet<string>>();
_renames = new Dictionary<Type, Dictionary<string, string>>();
}
public void IgnoreProperty(Type type, params string[] jsonPropertyNames)
{
if (!_ignores.ContainsKey(type))
_ignores[type] = new HashSet<string>();
foreach (var prop in jsonPropertyNames)
_ignores[type].Add(prop);
}
public void RenameProperty(Type type, string propertyName, string newJsonPropertyName)
{
if (!_renames.ContainsKey(type))
_renames[type] = new Dictionary<string, string>();
_renames[type][propertyName] = newJsonPropertyName;
}
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
{
var property = base.CreateProperty(member, memberSerialization);
if (IsIgnored(property.DeclaringType, property.PropertyName))
{
property.ShouldSerialize = i => false;
property.Ignored = true;
}
if (IsRenamed(property.DeclaringType, property.PropertyName, out var newJsonPropertyName))
property.PropertyName = newJsonPropertyName;
return property;
}
private bool IsIgnored(Type type, string jsonPropertyName)
{
if (!_ignores.ContainsKey(type))
return false;
return _ignores[type].Contains(jsonPropertyName);
}
private bool IsRenamed(Type type, string jsonPropertyName, out string newJsonPropertyName)
{
Dictionary<string, string> renames;
if (!_renames.TryGetValue(type, out renames) || !renames.TryGetValue(jsonPropertyName, out newJsonPropertyName))
{
newJsonPropertyName = null;
return false;
}
return true;
}
}
//忽略Foo示例中的number2
public class Foo
{
public string number1 { get; set; }
public string number2 { get; set; }
public string number3 { get; set; }
}
string Foojson = @"[
{
""number1"": 1,
""number2"": 12345678901234567890,
""number3"": 3
},
{
""number1"": 9,
""number2"": 12345678901234567890,
""number3"": 8
}
]";
var jsonResolverFoo = new PropertyRenameAndIgnoreSerializerContractResolver();
jsonResolverFoo.IgnoreProperty(typeof(Foo), "number2");
var serializerSettingsFoo = new JsonSerializerSettings();
serializerSettingsFoo.ContractResolver = jsonResolverFoo;
var deserializedJsonFoo = JsonConvert.DeserializeObject<List<Foo>>(Foojson, serializerSettingsFoo);