如何将json字符串反序列化为类

时间:2013-06-13 23:02:45

标签: c# json json.net

我有以下json字符串:

    {"Visits":[true,"DockedOnly","leftZone","0","500",0,0,0],
     "Weather":[true,"DockedOnly","leftZone","0","0",0,0,1],
     "ContactUs":[true,"DockedOnly","leftZone","0","317",0,0,2],
     "Birthdays":[true,"DockedOnly","middleZone","0","0",0,0,0],
     "Reminders":[true,"DockedOnly","middleZone","0","145",0,0,1],
     "Messages":[true,"DockedOnly","middleZone","0","0",0,0,2],
     "Availability":[true,"DockedOnly","middleZone","0","0",0,0,3],
     "Settings":[false,"DockedOnly","leftzone","0","155",0,0,0]}

无论如何反序列化为以下内容?

    [Serializable]
    public class WidgetProps
    {
        public bool Visible { get; set; }
        public string DockState { get; set; }
        public string Zone { get; set; }
        public string Top { get; set; }
        public string Left { get; set; }
        public int UnusedA { get; set; }
        public int UnusedB { get; set; }
        public int Position { get; set; }
    }

    [Serializable]
    public class WidgetLayout
    {
        public WidgetProps Visits { get; set; }
        public WidgetProps Weather { get; set; }
        public WidgetProps ContactUs { get; set; }
        public WidgetProps Birthdays { get; set; }
        public WidgetProps Reminders { get; set; }
        public WidgetProps Messages { get; set; }
        public WidgetProps Availability { get; set; }
        public WidgetProps Settings { get; set; }
    }

    public class Widget
    {
        public string WidgetName { get; set; }
        public WidgetProps props { get; set; }
    }

    List<Widget> MyWidgets;

我获得了json字符串,所以我无法改变它给我的方式,但也许我可以在得到它之后修补它以便它可以工作。

我试过了:

    string s = "{\"Visits\":[true,\"DockedOnly\",\"leftZone\",\"0\",\"500\",0,0,0],\"Weather\":[true,\"DockedOnly\",\"leftZone\",\"0\",\"0\",0,0,1],\"ContactUs\":[true,\"DockedOnly\",\"leftZone\",\"0\",\"317\",0,0,2],\"Birthdays\":[true,\"DockedOnly\",\"middleZone\",\"0\",\"0\",0,0,0],\"Reminders\":[true,\"DockedOnly\",\"middleZone\",\"0\",\"145\",0,0,1],\"Messages\":[true,\"DockedOnly\",\"middleZone\",\"0\",\"0\",0,0,2],\"Availability\":[true,\"DockedOnly\",\"middleZone\",\"0\",\"0\",0,0,3],\"Settings\":[false,\"DockedOnly\",\"leftzone\",\"0\",\"155\",0,0,0]}}";
    var sd = new JavaScriptSerializer().Deserialize < List<Widget>>(s);

    var sd = new JavaScriptSerializer().Deserialize < WidgetLayout >(s);

2 个答案:

答案 0 :(得分:0)

这不起作用,因为您尝试将数组反序列化为对象。 json.NET反序列化器无法进行转换。

因为你的json数组中有多种类型,你必须反序列化为最小的公分母,在本例中为object。从那里我建议编写一个方法,将每个索引分配给它在WidgetProps中的相应属性。

所以基本上,定义这个构造函数;

public WidgetProps(object[] props)
{
    Visible = (bool)props[0];
    DockState = (string)props[1];
    // ext
}

我会像WidgetDirty课那样对我进行初步的消毒。从那里你可以创建WidgetLayout的新实例,通过实例化它的每个属性,如myWidgetLayoutInstance.Visits = new WidgetProp(myWidgetDirtyInstance.Visits);我可能会在WidgetLayout构造函数中隐藏这个混乱,其中WidgetDirtydynamic它只是arg。

是的,这很恶心......但我不知道任何真正的替代品,因为json的设计与C#语言不太相容。如果您对此非常感兴趣,我可能会查看{{1}}类型。我没有在C#中使用它,也许永远都不会,但我知道用PHP这样的动态语言反序列化就不会有麻烦了。

答案 1 :(得分:0)

这不起作用,因为数组通常不会反序列化为对象。如果可能的话,我认为你应该修复JSON。如果您不能这样做,并且您正在使用JSON.NET,则可以为JsonConverter创建WidgetProps,以便手动将数组转换为对象:

class WidgetPropsConverter : JsonConverter
{
    public override void WriteJson(
        JsonWriter writer, object value, JsonSerializer serializer)
    {
        throw new NotSupportedException();
    }

    public override object ReadJson(
        JsonReader reader, Type objectType, object existingValue,
        JsonSerializer serializer)
    {
        var array = serializer.Deserialize<object[]>(reader);
        return new WidgetProps
        {
            Visible = (bool)array[0],
            DockState = (string)array[1],
            Zone = (string)array[2],
            Top = (string)array[3],
            Left = (string)array[4],
            Position = (int)(long)array[7]
        };
    }

    public override bool CanConvert(Type objectType)
    {
        return objectType == typeof(WidgetProps);
    }
}

然后你会像这样使用它:

var result = JsonConvert.DeserializeObject<WidgetLayout>(
    jsonString, new WidgetPropsConverter());