2016-02-05 48 views
2

如何生成類元數據的JSON。如何將類元數據轉換爲JSON字符串

例如。

C#類

public class Product 
{ 
    public int Id { get; set; } 
    public string Name { get; set; } 
    public bool IsActive { get; set; } 
    public Description Description { get; set; } 
} 

public class Description 
{ 
    public string Content { get; set; } 
    public string ShortContent { get; set; } 
} 

JSON

[ 
    { 
     "PropertyName" : "Id", 
     "Type" : "Int", 
     "IsPrimitive" : true 
    }, 
    { 
     "PropertyName" : "Name", 
     "Type" : "string", 
     "IsPrimitive" : true 
    }, 
    { 
     "PropertyName" : "IsActive", 
     "Type" : "bool", 
     "IsPrimitive" : true 
    }, 
    { 
     "PropertyName" : "Description", 
     "Type" : "Description", 
     "IsPrimitive" : false 
     "Properties" : { 
      { 
       "PropertyName" : "Content", 
       "Type" : "string", 
       "IsPrimitive" : true 
      }, 
      { 
       "PropertyName" : "ShortContent", 
       "Type" : "string", 
       "IsPrimitive" : true 
      } 
     } 
    }, 
] 
+0

所以,你不想要的值,類型只是說明? – Thomas

+0

@Thomas不,我只需要一個類的成員信息,以便我可以將它傳遞給消費者 –

+0

看到我的帖子,這應該做你想做的。 – Thomas

回答

3

如果你定義一個類,將映射您的JSON型號:

public class PropertyDescription 
{ 
    public string PropertyName { get; set; } 

    public string Type { get; set; } 

    public bool IsPrimitive { get; set; } 

    public IEnumerable<PropertyDescription> Properties { get; set; } 
} 

然後就是創建一個讀你的對象遞歸的屬性功能:

public static List<PropertyDescription> ReadObject(Type type) 
{ 
    var propertyDescriptions = new List<PropertyDescription>(); 
    foreach (var propertyInfo in type.GetProperties()) 
    { 
     var propertyDescription = new PropertyDescription 
     { 
      PropertyName = propertyInfo.Name, 
      Type = propertyInfo.PropertyType.Name 
     }; 

     if (!propertyDescription.IsPrimitive 
      // String is not a primitive type 
      && propertyInfo.PropertyType != typeof (string)) 
     { 
      propertyDescription.IsPrimitive = false; 
      propertyDescription.Properties = ReadObject(propertyInfo.PropertyType); 
     } 
     else 
     { 
      propertyDescription.IsPrimitive = true;    
     } 
     propertyDescriptions.Add(propertyDescription); 
    } 

    return propertyDescriptions; 
} 

您可以使用Json.Net序列化此函數的結果:

var result = ReadObject(typeof(Product)); 
var json = JsonConvert.SerializeObject(result); 

編輯:Linq的基於@AmitKumarGhosh的解決方案回答:

public static IEnumerable<object> ReadType(Type type) 
{ 
    return type.GetProperties().Select(a => new 
    { 
     PropertyName = a.Name, 
     Type = a.PropertyType.Name, 
     IsPrimitive = a.PropertyType.IsPrimitive && a.PropertyType != typeof (string), 
     Properties = (a.PropertyType.IsPrimitive && a.PropertyType != typeof(string)) ? null : ReadType(a.PropertyType) 
    }).ToList(); 
} 

... 

var result = ReadType(typeof(Product)); 
json = JsonConvert.SerializeObject(result); 
+0

優秀的解決方案!但不是傳遞對象,我想將Type傳遞給ReadObject函數。像'ReadObject(Product.GetType())' –

+0

我編輯了我的答案來傳遞類型而不是值。如果你沒有問題,你可以將問題標記爲答案嗎? – Thomas

+0

工作正常。非常感謝你:) –

2

試試這個,觀念得到對象字典中的所有元素。字段名稱和值。對於每個屬性,在類型,IsPrimitive等字典中創建其他元素(使用反射)。您可以使用遞歸進行拋出屬性,然後將此字典序列化爲JSON。

的位置例如:

Appending to JSON object using JSON.net

這樣的一個例子:

 var serialize = new Newtonsoft.Json.JsonSerializer(); 

     var dict = GetDic(new Description()); 

     serialize.Serialize(sr, dict); 

而且GetDcit實現:

private List<Dictionary<string, string>> GetDic(object obj) 
    { 
     var result= new List<Dictionary<string, string>>(); 

     foreach (var r in obj.GetType().GetProperties()) 
     { 
      result.Add(new Dictionary<string, string> 
      { 
       ["PropertyName"] = r.Name, 
       ["Type"] = r.PropertyType.Name, 
       ["IsPrimitive"] = r.GetType().IsPrimitive.ToString(), 
      }); 
     } 

     return result; 
    } 
+2

Upvoted爲您的功能的名稱! GetDic .... – Thomas

+0

爲什麼要將Json寫入文件? – Thomas

+1

無理由。它只是從我的項目複製粘貼。我更新我的答案。謝謝。 –

2

一個可能的解決方案 -

static void Main(string[] args) 
    { 
     var o = typeof(Product).GetProperties().Select(a => 
      { 
       if (a.PropertyType != null && (a.PropertyType.IsPrimitive || a.PropertyType == typeof(string))) 
       { 
        return MapType(a); 
       } 
       else 
       { 
        dynamic p = null; 
        var t = MapType(a); 
        var props = a.PropertyType.GetProperties(); 
        if (props != null) 
        { p = new { t, Properties = props.Select(MapType).ToList() }; } 

        return new { p.t.PropertyName, p.t.Type, p.t.IsPrimitive, p.Properties }; 
       } 

      }).ToList(); 

     var jsonString = JsonConvert.SerializeObject(o); 
    } 

    static dynamic MapType(PropertyInfo a) 
    { 
     return new 
     { 
      PropertyName = a.Name, 
      Type = a.PropertyType.Name, 
      IsPrimitive = a.PropertyType != null && a.PropertyType.IsPrimitive 
     }; 
    } 
+0

這是工作在這個層次結構,並提供了一個可能的解決方案。 –

相關問題