泛型 / JSON JavaScriptSerializer C#

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/304081/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 22:24:13  来源:igfitidea点击:

Generics / JSON JavaScriptSerializer C#

c#json

提问by Dave Mateer

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

我正在使用 VS2008Express 在 NET3.5SP1 中构建一个 winForms 应用程序。我正在尝试使用 System.Web.Script.Serialization 库反序列化一个对象。

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

错误是:数组的反序列化不支持类型“jsonWinForm.Category”。

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);
                }
            }
        }
    }
}

采纳答案by David Robbins

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 序列化工具,您可能想尝试JSON.Net

回答by Dave Mateer

I found my error.. should be:

我发现我的错误..应该是:

Cheers

干杯

JavaScriptSerializer serializer = new JavaScriptSerializer();

// create a generic list of categories
List<Category> listOfCategories = new List<Category>();

// deserialize as a list of Categories, and put into listOfCategories
listOfCategories = serializer.Deserialize<List<Category>>(s);

//iterate through list and display in text box
foreach (Category item in listOfCategories)
{
    textBox2.Text += item.categoryid.ToString() + "\r\n";
    textBox2.Text += item.name.ToString() + "\r\n";
    textBox2.Text += item.serverimageid.ToString() + "\r\n";
    textBox2.Text += item.dateuploaded.ToString() + "\r\n";
    textBox2.Text += item.enabled.ToString() + "\r\n";
}