Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

convert objects model to json in c#?

I have a object model I want to show as a json string, for example:

public class SectionD
{
    public string InsertID { get; set; }
    public int CaseReference { get; set; }
    public string AdditionalInfo { get; set; }
    public DateTime CreationDate { get; set; }
}

and I want to present this as a json object, like so:

{
  "class": "SectionD",
  "parameters": [
    {
      "key": "InsertID",
      "type": "string"
    },
    {
      "key": "CaseReference",
      "type": "int"
    },
    {
      "key": "AdditionalInfo",
      "type": "string"
    },
    {
      "key": "CreationDate",
      "type": "DateTime"
    }
  ]
}

The data is being stored as a json string in a database, and I want to provide a list of fields and types to someone who would be making database views on that data.

google provides a lot of hits for querying the contents on the model, but I can't find anything for looking at the object itself.

Thanks

question from:https://stackoverflow.com/questions/66050799/convert-objects-model-to-json-in-c

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

How about something simple like:

public class ReflectedPropertyInfo
{
    [JsonProperty("key")]
    public string Key { get; set; }
    [JsonProperty("type")]
    public string Type { get; set; }
}
public class ReflectJson
{
    public static string ReflectIntoJson<T>() where T : class
    {
        var type = typeof(T);
        var className = type.Name;
        var props = type.GetProperties(BindingFlags.Instance | BindingFlags.Public);
        var propertyList = new List<ReflectedPropertyInfo>();
        foreach (var prop in props)
        {
            propertyList.Add(new ReflectedPropertyInfo{Key =prop.Name, Type =prop.PropertyType.Name});
        }

        var result = JsonConvert.SerializeObject(new {@class = className, parameters = propertyList}, Formatting.Indented);
        return result;
    }
}

It uses Reflection, as suggested by @dbc. After getting the type name, it gets a collection of properties and then builds up a anonymous type containing the information in the correct format, and then serializes it. The result looks like:

{
  "class": "SectionD",
  "parameters": [
    {
      "key": "InsertID",
      "type": "String"
    },
    {
      "key": "CaseReference",
      "type": "Int32"
    },
    {
      "key": "AdditionalInfo",
      "type": "String"
    },
    {
      "key": "CreationDate",
      "type": "DateTime"
    }
  ]
}

The only difference (that I see) is that it uses the actual "Int32" as the type name for integers, rather than the C# alias "int".


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...