serialize/deserialize List to JSON

后端 未结 1 857
野的像风
野的像风 2021-02-10 01:21

I want to be able to serialize/deserialize a generic list what I so far is this

    public static string ToJson(this object obj, int recursionDepth = 100) 
    {         


        
相关标签:
1条回答
  • 2021-02-10 02:14

    Try this on for size:

    public static T ToObject<T>(this string obj, int recursionDepth = 100)
    {
        JavaScriptSerializer serializer = new JavaScriptSerializer();
        serializer.RecursionLimit = recursionDepth;
        return serializer.Deserialize<T>(obj);
    }
    

    Then use it like this:

    mystring.ToObject<List<MyClass>>();
    
    0 讨论(0)
提交回复
热议问题