下面的代码只有FIRST和LAST名称。DeserializeObject正在查找FIRST MIDDLE LAST。所以约翰·史密斯会出现,但约翰·P·史密斯不会。有没有办法只查找名字和姓氏?
#region Fields
private string firstName;
private string middleInitial;
private string lastName;
#endregion
#region Properties
public string FirstName
{
get { return firstName; }
set { firstName = value; }
}
public string MiddleInitial
{
get { return middleInitial; }
set { middleInitial = value; }
}
public string LastName
{
get { return lastName; }
set { lastName = value; }
}
#endregion
#region Constructors
public NamedIndividual()
{
}
var namedIndividual = JsonConvert.DeserializeObject<NamedIndividual>(cov.GetAttribute("individual").Value);
JSON.NET有一种很好的方法,可以将属性标记为非必需属性-您可以使用DataContract
和DataMember
属性。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using Newtonsoft.Json;
namespace ConsoleApplication1
{
[DataContract]
public class NamedIndividual
{
#region Fields
private string firstName;
private string middleInitial;
private string lastName;
#endregion
#region Properties
[DataMember(IsRequired = true)]
public string FirstName
{
get { return firstName; }
set { firstName = value; }
}
[DataMember(IsRequired = false)]
public string MiddleInitial
{
get { return middleInitial; }
set { middleInitial = value; }
}
[DataMember(IsRequired = true)]
public string LastName
{
get { return lastName; }
set { lastName = value; }
}
#endregion
public NamedIndividual()
{
}
}
class Program
{
static void Main(string[] args)
{
string name = "{'FirstName':'David', 'MiddleInitial':'Q', 'LastName':'Hoerster'}";
string name1 = "{'FirstName':'David', 'LastName':'Hoerster'}";
var obj = JsonConvert.DeserializeObject<NamedIndividual>(name);
var obj1 = JsonConvert.DeserializeObject<NamedIndividual>(name1);
Console.WriteLine(obj.MiddleInitial);
Console.WriteLine("{0} {1} {2}",obj1.FirstName, obj1.MiddleInitial, obj1.LastName);
}
}
}
因此,我将MiddleInitial
标记为可选属性,这样我的JSON字符串就可以包含它或不包含它。如果我将属性标记为必需(IsRequired=true
),那么我的JSON字符串最好具有该属性,否则将引发异常。
UPDATE我可能暗示您需要使用DataContract
属性,以便使某些属性成为不需要或不需要的属性。JSON.net也有自己的一组属性,加上@L.B.下面的评论,不使用任何属性提供了默认行为,这是不需要的。下面是一个使用JsonObject
属性的快速示例:
[JsonObject(MemberSerialization=MemberSerialization.OptIn)]
public class NamedIndividual1
{
[JsonProperty(Required = Required.Always)]
public string FirstName { get; set; }
[JsonProperty(Required = Required.Default)]
public string MiddleInitial { get; set; }
[JsonProperty(Required = Required.Always)]
public string LastName { get; set; }
}
class Program
{
static void Main(string[] args)
{
string name2 = "{'FirstName':'David', 'LastName':'Hoerster'}";
var obj2 = JsonConvert.DeserializeObject<NamedIndividual>(name2);
Console.WriteLine("{0} {1} {2}", obj2.FirstName, obj2.MiddleInitial, obj2.LastName);
}
}
(我也不能使用任何属性使所有内容都是可选的。)
希望这能有所帮助!
您可以将构造函数更改为空构造函数或将其kepp为空构造函数,并创建和重载构造函数
public NamedIndividual()
{
//empty Constructor
}
//overloaded constructor
public NamedIndividual(string first, string last)
{
this.firstname = first;
this.lastname = last;
}
// or pass in the middle as an option