2013-10-12 109 views
1

我想我的類類型的JSON表示,所以我可以將它們插入到客戶端模式。這是我的服務器端的C#類的樣子:將類類型序列化爲JSON?

public class Person 
{ 
    public int ID { get; set; } 

    [Required(ErrorMessage = "Name is required.")] 
    public string Name { get; set; } 

    public DateTime Birthday { get; set; } 

    public bool IsMarried { get; set; } 
} 

如何序列化到這個?:

{ 
    fields: { 
     "ID": { 
      type: "number" 
     }, 
     "Name": { 
      type: "string", 
      validation: { 
       required: true, 
       message: "Name is required.", 
      } 
     }, 
     "Birthday": { 
      type: "date" 
     }, 
     "IsMarried": { 
      type: "bool" 
     } 
    } 
} 
+0

爲什麼JavaScript的標籤?你想在服務器上這樣做,對吧? – user2736012

+0

http://stackoverflow.com/questions/7167950/how-to-serialize-from-c-sharp-to-json-a-list-containing-a-list-containing-an-arr – mconlin

+0

你想創建json模式? – PSL

回答

2

有沒有什麼(據我所知)現成的,在JSON.NET庫,但它是相當簡單的使用一些反射代碼來實現它:

public class StackOverflow_19336832 
{ 
    public class RequiredAttribute : Attribute 
    { 
     public string ErrorMessage { get; set; } 
    } 

    public class Person 
    { 
     public int ID { get; set; } 

     [Required(ErrorMessage = "Name is required.")] 
     public string Name { get; set; } 

     public DateTime Birthday { get; set; } 

     public bool IsMarried { get; set; } 
    } 

    static JObject GetSchema(Type type) 
    { 
     var result = new JObject(); 
     foreach (var prop in type.GetProperties()) 
     { 
      var name = prop.Name; 
      var propType = prop.PropertyType; 
      var field = new JObject(); 
      result.Add(name, field); 
      switch (Type.GetTypeCode(propType)) 
      { 
       case TypeCode.Boolean: 
        field.Add("type", "bool"); 
        break; 
       case TypeCode.Int16: 
       case TypeCode.Int32: 
       case TypeCode.Int64: 
       case TypeCode.UInt16: 
       case TypeCode.UInt32: 
       case TypeCode.UInt64: 
       case TypeCode.Double: 
       case TypeCode.Single: 
        field.Add("type", "number"); 
        break; 
       case TypeCode.String: 
        field.Add("type", "string"); 
        break; 
       case TypeCode.DateTime: 
        field.Add("type", "date"); 
        break; 
       default: 
        throw new ArgumentException("Don't know how to generate schema for property with type " + propType.FullName); 
      } 

      var req = prop.GetCustomAttributes(typeof(RequiredAttribute), false).FirstOrDefault() as RequiredAttribute; 
      if (req != null) 
      { 
       var validation = new JObject(); 
       field.Add("validation", validation); 
       validation.Add("required", true); 
       if (!string.IsNullOrEmpty(req.ErrorMessage)) 
       { 
        validation.Add("message", req.ErrorMessage); 
       } 
      } 
     } 

     return result; 
    } 

    public static void Test() 
    { 
     var schema = GetSchema(typeof(Person)); 
     Console.WriteLine(schema); 
    } 
} 
+0

有一個JsonSchemaGenerator將從一個類型創建一個JsonSchema。然後你可以通過ToString()來獲得JSON的JsonSchema - http://james.newtonking.com/json/help/index.html?topic=html/AllMembers_T_Newtonsoft_Json_Schema_JsonSchemaGenerator.htm –

+0

這很好,謝謝指出! – carlosfigueira

-1

看看這裏瞭解如何使用JavaScriptSerializer得到一些想法任務完成。還有其他序列化器,但是這個序列化器很好,鏈接提供瞭如何實現和使用它的例子。

http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.aspx

+0

OP正試圖從他的類定義中創建JSON模式,而不是序列化類的一個實例。您引用的鏈接並未演示如何使用JavaScriptSerializer來執行此操作。 –

+0

啊,我的壞。感謝您指出了這一點! – Haney

相關問題