我可以分离JsonConvert对象吗?

时间:2012-01-10 18:56:24

标签: c# .net json attributes json.net

我有以下代码,只有FIRST和LAST名称。 DeserializeObject正在寻找第一个中期。所以约翰史密斯会表现出来,而不是约翰史密斯。有没有办法只查找名字和姓氏?

#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);

2 个答案:

答案 0 :(得分:2)

JSON.NET有一种很好的方法可以让你将属性标记为不需要 - 你可以使用DataContractDataMember属性。

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字符串会更好地拥有该属性,否则将抛出异常。

更新我可能暗示您需要使用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);
    }
}

(我也不能使用任何属性作为可选项。)

希望这有帮助!

答案 1 :(得分:1)

您可以更改构造函数或将其保留为空构造函数并创建并重载构造函数

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