如何将类元数据转换为JSON字符串 [英] How to get Class Metadata into JSON string

查看:62
本文介绍了如何将类元数据转换为JSON字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何生成Class元数据的JSON.

How to generate JSON of Class meta data.

例如.

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
            }
        }
    },
]

推荐答案

如果定义一个将映射您的Json模型的类:

If you define a class that will map your Json Model:

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

    public string Type { get; set; }

    public bool IsPrimitive { get; set; }

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

然后创建一个递归读取对象属性的函数:

And then just create a function that read the properties of your object recursively:

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 序列化此函数的结果:

You can use Json.Net to serialize the result of this function :

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

基于@AmitKumarGhosh的Linq解决方案:

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

这篇关于如何将类元数据转换为JSON字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆