c# - JavaScriptSerializer C# 和泛型(我认为是新手犯的错误!)

标签 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"
    }
]
}

然后很容易反序列化:(哎呀..有点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
            }
        ]
    }
}

最佳答案

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

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 指针,我永远找不到它!

关于c# - JavaScriptSerializer C# 和泛型(我认为是新手犯的错误!),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/325020/

相关文章:

c# - 如何在 visual studio 中重新创建这样的列表?

c# - 在 wpf 中制作 StackPanel 及其内容 "draggable"?

c# - 使用导航将 SQL Distinct、Count 和 GroupBy 转换为 Linq 查询

c# - Azure 函数 - 在新 blob 上设置 CloudBlockBlob 元数据

c# - ASP .Net MapRequestHandler 慢

c# - 更改 gridView 位置 WinRT Xaml

c# - 如何保留在使用 List.Clear() 之前已经创建的列表

c# - 如何从数据库字段中获取 <li> 的值?

c# System.guid 不包含 Parse 的定义

c# - 在没有泛型类约束的情况下,将泛型类型与其默认值进行比较会产生编译时错误