C# JSON String Deserialization

前端 未结 3 2050
生来不讨喜
生来不讨喜 2021-01-13 00:15

I am really struggling to find any examples of JSON Deserialization in C# where the first property in each JSON item is a different ID.

    {
       \"121120         


        
3条回答
  •  情话喂你
    2021-01-13 00:47

    Using System.Web, I have a little method that seems to make its way into every project that uses JSON for data storage:

    private T UnpackJson(object data)
    {
        var serializer = new JavaScriptSerializer();
        string rawJSON = serializer.Serialize(data);
    
        var result = serializer.Deserialize(rawJSON);
        return result;
    }
    

    ...Where T is some type with elements that match the schema that you are deserializing. For your blob that is given in the top, I'd suggest something like:

    // Using my above method...
    var x = UnpackJson(blob);
    
    public class Foo
    {
        public string ID;
        public Bar SubItem;
    }
    
    public class Bar
    {
        public int Topic_ID;
        public int Subject_ID;
        public string subject; 
    }
    

    ...Note that your variables names should totally match the names of attributes in the JSON blob! I certainly hope this works (note that while I use the method and know for certain that it will deserialize a blob to a class, this implementation is untested.)

提交回复
热议问题