2012-08-16 62 views
4

我正在使用newtonsoft的庫反序列化一個稍微複雜的對象,並遇到了未安裝MetaData對象中的Id的情況,每隔一個值似乎都沒有問題。所有主要對象都來自 類,它具有核心屬性,如Id。有沒有人有任何建議,這裏有什麼問題?當使用JSON和C反序列化對象時丟失了guid#

這是我使用來測試JSON,頂級對象是一個頁面:

{ 
    "CreatedOn": "2012-07-30T13:42:45+01:00", 
    "CreatedBy": "33576D49-B799-4326-812C-D6A3C47B7035", 
    "Name": "Icecream", 
    "Metadata": [ 
    { 
     "CreatedOn": "2012-07-30T14:06:31+01:00", 
     "ModifiedOn": "2012-07-30T14:06:31+01:00", 
     "ModifiedBy": "53D947AC-0D7B-42C4-929E-DAFC25932784", 
     "Name": "Colour", 
     "Value": "Blue", 
     "NodeReference": "834a2734-e807-4eb7-a242-c26d785681f3", 
     "Scope": "Draft", 
     "Id": "032488bd-158c-4064-93ec-44bd11c4e642", 
     "CreatedBy": "53D947AC-0D7B-42C4-929E-DAFC25932784" 
    }, 
    { 
     "CreatedOn": "2012-07-30T13:48:49+01:00", 
     "ModifiedOn": "2012-07-30T14:23:20+01:00", 
     "ModifiedBy": "10E0821E-1982-4C24-B9E1-35FAF9A547BB", 
     "Name": "Topping", 
     "Value": "Chocolate sprinkles", 
     "NodeReference": "834a2734-e807-4eb7-a242-c26d785681f3", 
     "Scope": "Live", 
     "Id": "241afb40-0d83-4599-a05e-aec1a6cdef41", 
     "CreatedBy": "CE1F79DE-9ECF-456F-8C17-D9B7E4B6FCA7" 
    } 
    ], 
    "WikiResources": [  
    ], 
    "FileResources": [  
    ], 
    "Links": [  
    ], 
    "Id": "834a2734-e807-4eb7-a242-c26d785681f3" 
} 

這裏是我的代碼(簡體)版本:

public abstract class Model { 

    protected Guid? _id; 
    public Guid? Id { 
     get { return _id;} 
     set { _id = value; IsDirty = true; } 
    } 

    private string _name; //e.g page Heading 
    public string Name { 
     get { return _name; } 
     set { _name = value; IsDirty = true; } 
    } 

    private DateTime _createdOn; 
    public DateTime CreatedOn { 
     get { return _createdOn; } 
     set { _createdOn = value; IsDirty = true; } 
    } 

    private string _createdBy; 
    public string CreatedBy { 
     get { return _createdBy; } 
     set { _createdBy = value; IsDirty = true; } 
    } 

    [JsonIgnore] 
    public bool IsDirty { 
     get; 
     set; 
    } 

    [JsonIgnore] 
    public bool IsNew { 
     get; 
     set; 
    } 

    private List<MetaDatum> _metadata; 
    public List<MetaDatum> MetaData { 
     get { 
      return _metadata; 
     } 
     set { 
      _metadata = value; 
     } 
    } 
} 



public class MetaDatum : Model, IModel { 

    //other properties and methods omitted 

    #region construction 

     public MetaDatum() { 
      IsNew = false; 
      webservice = WebserviceManager.GetWebservice(); 

     } 

     //quick factory method for creating a name value pair 
     public static MetaDatum Create(string name, string value, ScopeType scope) { 
      MetaDatum md = new MetaDatum(); 
      md.Name = name;   
      md.CreatedOn = DateTime.Now; 
      md.Scope = scope; 
      md.Value = value.ToString(); 
      md.IsNew = true; 
      md.IsDirty = true; 
      return md; 
     } 

     #endregion 

} 

public class Page : Model, IModel { 

    //other properties and methods omitted 

    public static Page Deserialize(string text) { 

      if (text == "" || text == null) 
       return null; 

      dynamic serializedObj = JsonConvert.DeserializeObject(text); 
      Page convertedObject = JsonConvert.DeserializeObject<Page>(text); 

      return convertedObject; 
     } 

} 

回答

0

我有在反序列化之後手動獲取Ids,通過將其設置爲動態對象並鑽入它。但是也許有一種更好的方法可以一氣呵成呢?

int i = 0;   
dynamic serializedObj = JsonConvert.DeserializeObject(text); 
foreach (dynamic item in serializedObj.Metadata) { 
    convertedObject.MetaData[i++].Id = Guid.Parse(item.Id.ToString()); 
} 
0

驗證我的意思是MediaTypeWithQualityHeaderValue(「text/plain」)<您發送的報頭的數據類型 - 因爲它可以是一個應用程序/ JSON或文本/ JSON,這種類型是可以改變的序列化可以做到