首页 > 解决方案 > 我可以将 json 数据反序列化为静态类吗

问题描述

所以我想做的是将json数据带入一个静态类。但我目前的方法如图所示是有 2 个类,一个用于获取数据,然后另一个静态类,相同,用于保存数据

这里的目标是拥有静态数据类

internal class _iData
{       
    public string CompanyName { get; set; }
    public string UserName { get; set; }
    public string Description { get; set; }
    public string Shelf { get; set; }
    public string Quantiy { get; set; }
}

public static class ItemData 
{
    public static string CompanyName { get; set; }
    public static string UserName { get; set; }
    public static string Description { get; set; }
    public static string Shelf { get; set; }
    public static string Quantiy { get; set; }
}

_iData ob = js.Deserialize<_iData>(objText);
// Move data from instantiated class to static class
ItemData.CompanyName = ob.CompanyName;
ItemData.Description = ob.Description;
ItemData.Quantiy = ob.Quantiy;
ItemData.Shelf = ob.Shelf;
ItemData.UserName = ob.UserName;

然后我复制了不是世界末日的数据,但我觉得我错过了一些东西

标签: c#

解决方案


您不能反序列化为static.

反序列化时,您将获得一个instancestatic类不能有实例(因为它们是static


推荐阅读