将 JSON 对象反序列化为 List<type>不使用 asmx 服务 [英] Deserializing JSON objects as List&lt;type&gt; not working with asmx service

查看:37
本文介绍了将 JSON 对象反序列化为 List<type>不使用 asmx 服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在反序列化我的 JSON 字符串时遇到问题.我有一个 person 类型的类,它具有 int、名字和姓氏类型的序列号的公共属性.我想以 JSON 格式传递这些对象的数组,并将它们反序列化为列表,以便我可以在服务器上遍历它们,但是 ASP.NET 表示不支持将它们反序列化为数组.我已经验证了我生成的 JSON,它是有效的.ASP.NET 在反序列化之前需要具备的 JSON 有什么特别之处吗?有趣的是,如果我序列化一个列表<person>JSON 对象,它看起来与我生成的 JSON 完全一样.我一定是遗漏了一些东西……澄清一下,我正在使用 ASP.NET Ajax 库来反序列化.这是我从网络服务中得到的:

I am having trouble deserializing my JSON string. I have a class of type person with public properties for sequence number of type int, first name, and last name. I want to pass an array of these objects in JSON format and have them deserialized as a list so I can loop through them on the server, but ASP.NET says something about not being supported to be deserialized as an array. I have validated the JSON I am producing, and it is valid. Is there something special about the JSON that ASP.NET needs to have before it can deserialize? The funny thing is if I serialize a list<person> object to JSON it looks exactly like the JSON I am producing. I must be missing something... To clarify, I'm using the ASP.NET Ajax library to deserialize. This is what I get back from the web service:

{"Message":"Type u0027System.Collections.Generic.IDictionary`2[[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Object,mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]u0027 不支持数组的反序列化."

{"Message":"Type u0027System.Collections.Generic.IDictionary`2[[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Object, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]u0027 is not supported for deserialization of an array."

实际上不幸的是,这似乎与反序列化无关,您似乎无法将 JSON 对象数组传递给 asmx Web 服务.我对么?如果您不能这样做,是否可以将一组 JSON 对象传递给 Web 服务,并使用 ASP.NET 和 C# 在服务器上处理它们?

Actually unfortunately this doesn't seem to have anything to do with deserializing, it appears that you can't pass an array of JSON objects to an asmx web service. Am I correct? If you can't do that, is it possible to pass a collection of JSON objects to a web service and have them processed on the server with ASP.NET and C#?

好的,这是我的代码.这是person类:

OK, here is my code. Here is the person class:

public class person
{
    public person()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    public int seq
    {
        get;
        set;
     }

    public string firstName
    {
        get;
        set;
     }
     public string lastName
     {
        get;
        set;
     }

}  

这是我的 JSON 字符串:

And here is my JSON string:

[{"seq":1,"firstName":"Chris","lastName":"Westbrook"},
{"seq":2,"firstName":"sayyl","lastName":"westbrook"}]  

这是我正在使用的代码

    [WebMethod]
    public void updatePeople(string json)
    {
        IList<person> people = 
         new JavaScriptSerializer().Deserialize<IList<person>>(json);

        //do stuff...
    }

推荐答案

我想通了.我没有将我的 JSON 包装在像 ASP.NET Ajax 需要的对象中.对于此问题的未来查看者,所有 JSON 对象在发送到 Web 服务之前必须使用主对象包装.最简单的方法是在 JavaScript 中创建对象并使用类似 json2.js 的东西来字符串化它.此外,如果使用 asmx Web 服务,则对象必须具有 __type 属性才能正确序列化.一个例子可能是:

I figured it out. I wasn't wrapping my JSON in an object like ASP.NET Ajax requires. For future viewers of this question, all JSON objects must be wrapped with a main object before being sent to the web service. The easiest way to do this is to create the object in JavaScript and use something like json2.js to stringify it. Also, if using an asmx web service, the objects must have a __type attribute to be serialized properly. An example of this might be:

var person=new object;
person.firstName="chris";
person.lastName="Westbrook";
person.seq=-1;
var data=new object;
data.p=person;
JSON.stringify(data);

这将创建一个名为 p 的对象,它将包装一个 person 对象.然后可以将其链接到 Web 服务中的参数 p.person 类型的列表也是类似的,接受使用一组人员而不是单个对象.我希望这对某人有所帮助.

This will create an object called p that will wrap a person object. This can then be linked to a parameter p in the web service. Lists of type person are made similarly, accept using an array of persons instead of just a single object. I hope this helps someone.

这篇关于将 JSON 对象反序列化为 List<type>不使用 asmx 服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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