泛型/ JSON的JavaScriptSerializer C# [英] Generics / JSON JavaScriptSerializer C#

查看:154
本文介绍了泛型/ JSON的JavaScriptSerializer C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用VS2008Ex preSS建立一个WinForms应用程序中NET3.5SP1。我试图用System.Web.Script.Serialization库反序列化对象。

该错误是:类型jsonWinForm.Category'不支持数组的反序列化

干杯!

 使用系统;
使用System.Collections.Generic;
使用System.ComponentModel;
使用System.Data这;
使用System.Drawing中;
使用System.Linq的;
使用System.Text;
使用System.Windows.Forms的;
使用的System.Web;
使用System.Net;
使用System.IO;
使用System.Web.Script.Serialization;命名空间jsonWinForm {
    公共类目录
    {
        公众诠释类别ID;
        公共字符串名称;
        公众诠释serverimageid;
        公众的DateTime dateuploaded;
        公共BOOL启用;
    }    公共部分Form1类:表格
    {
        公共Form1中()
        {
            的InitializeComponent();
        }        私人无效的button1_Click(对象发件人,EventArgs的发送)
        {
            使用(Web客户端的客户端=新的WebClient())
            {
                //处理请求头(可选)
                client.Headers.Add(用户代理,Mozilla的/ 4.0(兼容; MSIE 6.0; Windows NT的5.2; .NET CLR 1.0.3705;));
                字符串targetUri =htt​​p://www.davemateer.com/ig/genius/category.php;                //执行请求和响应读取字符串形式的安慰
                使用(StreamReader的读者=新的StreamReader(client.OpenRead(targetUri)))
                {
                    字符串s = reader.ReadToEnd();
                    textBox1.Text =秒;                    猫类=新类别();
                    串行的JavaScriptSerializer =新的JavaScriptSerializer();                    //失败了
                    //类型jsonWinForm.Category'不支持数组的反序列化。
                    serializer.Deserialize<类别及GT;(S);
                }
            }
        }
    }
}


解决方案

这是伟大的你找到你的错误。如果您正在寻找另一种工具,JSON序列化,你可能想尝试 JSON.Net

I'm building a winForms app in NET3.5SP1 using VS2008Express. Am trying to deserialize an object using the System.Web.Script.Serialization library.

The error is: Type 'jsonWinForm.Category' is not supported for deserialization of an array.

Cheers!

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Web;
using System.Net;
using System.IO;
using System.Web.Script.Serialization;

namespace jsonWinForm {
    public class Category
    {
        public int categoryid;
        public string name;
        public int serverimageid;
        public DateTime dateuploaded;
        public bool enabled;
    }

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            using (WebClient client = new WebClient())
            {
                //manipulate request headers (optional)
                client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
                string targetUri = "http://www.davemateer.com/ig/genius/category.php";

                //execute request and read response as string to console
                using (StreamReader reader = new StreamReader(client.OpenRead(targetUri)))
                {
                    string s = reader.ReadToEnd();
                    textBox1.Text = s;

                    Category cat = new Category();
                    JavaScriptSerializer serializer = new JavaScriptSerializer();

                    // this fails with a 
                    //Type 'jsonWinForm.Category' is not supported for deserialization of an array.
                    serializer.Deserialize<Category>(s);
                }
            }
        }
    }
}

解决方案

It's great you found your error. If you are looking for another tool for JSON serialization you might want to try JSON.Net.

这篇关于泛型/ JSON的JavaScriptSerializer C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆