JavaScriptSerializer C#和Generics(我认为新手错误!)

时间:2008-11-28 03:26:40

标签: c#

WinForms C#..我正在以下面的格式(消息的底部)获取一些JSON,并尝试使用反序列化:

使用System.Web.Script.Serialization;

当我只是这个json回来了:

{
"objects": [
    {
        "categoryid": "1",
        "name": "funny",
        "serverimageid": "1",
        "dateuploaded": "2008-11-17 16:16:41",
        "enabled": "1"
    },
    {
        "categoryid": "2",
        "name": "happy",
        "serverimageid": "2",
        "dateuploaded": "2008-11-17 16:17:00",
        "enabled": "1"
    },
    {
        "categoryid": "3",
        "name": "sad",
        "serverimageid": "3",
        "dateuploaded": "2008-11-16 16:17:13",
        "enabled": "1"
    }
]
}

然后很容易反序列化:( yikes ..有点hacky)

// s is the string
s = s.Remove(0, 11);
// last }
int stringLength = s.Length;
s = s.Remove(stringLength - 1, 1);

listOfCategories = serializer1.Deserialize<List<Category>>(s);

其中

public class Category
    {
        public int categoryID;
        public string name;
        public int imageID;
        public DateTime dateUpdated;
        public int isActive;
        public int displayOrder;
    }

然而现在,我被困住了!尝试过列表清单......但无法到达任何地方..

非常感谢任何帮助。

{
    "objects": {
        "categories": [
            {
                "name": "Congratulations",
                "imageID": "1",
                "isActive": "1",
                "displayOrder": "0",
                "dateUpdated": "2008-11-27 00:00:00"
            },
            {
                "name": "Animals",
                "imageID": "2",
                "isActive": "1",
                "displayOrder": "0",
                "dateUpdated": "2008-11-26 00:00:00"
            },
            {
                "name": "Romance",
                "imageID": "3",
                "isActive": "1",
                "displayOrder": "0",
                "dateUpdated": "2008-11-24 00:00:00"
            }
        ],
        "present": [
            {
                "presentID": "1",
                "name": "Tiger",
                "categoryID": "2",
                "imageID": "1",
                "dateUpdated": "2008-11-27",
                "isActive": "1",
                "isAnimated": null,
                "isInteractive": null,
                "isAdaptive": null,
                "webLinkURL": null
            },
            {
                "giphtID": "2",
                "name": "Donkey",
                "categoryID": "2",
                "imageID": "2",
                "dateUpdated": "2008-11-27",
                "isActive": "1",
                "isAnimated": null,
                "isInteractive": null,
                "isAdaptive": null,
                "webLinkURL": null
            },
            {
                "giphtID": "3",
                "name": "Elephant",
                "categoryID": "2",
                "imageID": "3",
                "dateUpdated": "2008-11-27",
                "isActive": "1",
                "isAnimated": null,
                "isInteractive": null,
                "isAdaptive": null,
                "webLinkURL": null
            }
        ]
    }
}

2 个答案:

答案 0 :(得分:4)

这似乎工作正常(而且没有古怪的字符串修剪!):

using System;
using System.Collections.Generic;
using System.Web.Script.Serialization;

class Program
{
    static void Main( string[] args )
    {
        string json = System.IO.File.ReadAllText( "../../input.json" );

        var serializer = new JavaScriptSerializer();
        Structure jsonStructure = serializer.Deserialize<Structure>( json );
        System.Diagnostics.Debugger.Break();
    }
}

class Structure
{
    public StructureObjects objects;
}

class StructureObjects
{
    public List<StructureCategory> categories;
    public List<StructurePresent> present;
}

class StructureCategory
{
    public string name;
    public int imageID;
    public DateTime dateUpdated;
    public int isActive;
    public int displayOrder;
}

class StructurePresent
{
    public int presentID;
    public string name;
    public int categoryID;
    public int imageID;
    public DateTime dateUpdated;
    public int isActive;
    public int? isAnimated;
    public int? isInteractive;
    public int? isAdaptive;
    public Uri webLinkURL;
}

感谢System.Web.Script.Serialization指针,我永远都找不到!

答案 1 :(得分:1)

修改:以下内容适用于DataContractJsonSerializer,但可以使用OP中的JavascriptSerializer。所以这只是为了参考。

[DataContract]
class Foo
{
    [DataMember(Name = "objects")]
    public Bar Bar { get; set; }
}

[DataContract]
class Bar
{
    public Bar() {
        Categories = new List<Category>();
        Present = new List<Present>();
    }
    [DataMember(Name = "categories")]
    public List<Category> Categories { get; private set; }
    [DataMember(Name = "present")]
    public List<Present> Present { get; private set; }
}
[DataContract]
class Category
{
    [DataMember(Name = "name")]
    public string Name {get;set;}
    [DataMember(Name = "imageID")]
    public int ImageID {get;set;}
    [DataMember(Name = "isActive")]
    public int IsActive {get;set;}
    [DataMember(Name = "displayOrder")]
    public int DisplayOrder {get;set;}
    [DataMember(Name = "dateUpdated")]
    public string DateUpdated {get;set;}
}
[DataContract]
class Present
{
    [DataMember(Name = "presentID")]
    public int PresentID {get;set;}
    [DataMember(Name = "name")]
    public string Name {get;set;}
    [DataMember(Name = "categoryID")]
    public int CategoryID {get;set;}
    [DataMember(Name = "imageID")]
    public int ImageID {get;set;}    
    [DataMember(Name = "dateUpdated")]
    public string DateUpdated {get;set;}
    [DataMember(Name = "isActive")]
    public int IsActive {get;set;}
    [DataMember(Name = "isAnimated")]
    public int? IsAnimated {get;set;}
    [DataMember(Name = "isInteractive")]
    public int? IsInteractive {get;set;}
    [DataMember(Name = "isAdaptive")]
    public int? IsAdaptive {get;set;}
    [DataMember(Name = "webLinkURL")]
    public string WebLinkUrl {get;set;}
}