当前位置:  开发笔记 > 编程语言 > 正文

泛型/ JSON JavaScriptSerializer C#

如何解决《泛型/JSONJavaScriptSerializerC#》经验,为你挑选了2个好方法。

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

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

干杯!

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

Dave Mateer.. 12

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

干杯

JavaScriptSerializer serializer = new JavaScriptSerializer();

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

// deserialize as a list of Categories, and put into listOfCategories
listOfCategories = serializer.Deserialize>(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";
}

如果要将其替换为Deserialize的返回结果,则无需将listOfCategories初始化为新的List (). (14认同)


David Robbin.. 10

你发现错误很棒.如果您正在寻找JSON序列化的另一个工具,您可能想尝试JSON.Net.



1> Dave Mateer..:

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

干杯

JavaScriptSerializer serializer = new JavaScriptSerializer();

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

// deserialize as a list of Categories, and put into listOfCategories
listOfCategories = serializer.Deserialize>(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";
}


如果要将其替换为Deserialize的返回结果,则无需将listOfCategories初始化为新的List ().

2> David Robbin..:

你发现错误很棒.如果您正在寻找JSON序列化的另一个工具,您可能想尝试JSON.Net.

推荐阅读
罗文彬2502852027
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有