反序列化JSON对象的名单,LT;类型>与ASMX服务工作 [英] Deserializing JSON objects as List<type> not working with asmx service

查看:287
本文介绍了反序列化JSON对象的名单,LT;类型>与ASMX服务工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法反序列化我的JSON字符串。我有一个类类型的与int类型的序列号,名字和姓氏的公共属性。我想通过JSON格式这些对象的数组,并让他们反序列化的列表,以便我可以通过这些循环的服务器上,但ASP.NET说一些关于不被支持反序列化作为数组。我已经验证我制造JSON和它是有效的。有什么特殊之处,ASP.NET需要有才可以反序列化JSON?有趣的是,如果我序列化一个列表<人>对象到JSON它看起来完全像我产生JSON。我必须失去了一些东西......为了澄清,我使用ASP.NET Ajax库进行反序列化。这是我从网络服务回报:


  

{消息:类型\\ u0027System.Collections.Generic.IDictionary`2 [System.String,mscorlib程序,版本= 2.0.0.0,文化=中性公钥= b77a5c561934e089],[System.Object的,mscorlib中,版本= 2.0.0.0,文化=中性公钥= b77a5c561934e089]] \\不支持数组的反序列化u0027。


其实不幸的是这似乎不具备任何与反序列化,看来,你不能传递JSON对象的数组到ASMX Web服务。我对么?如果你不能做到这一点,是有可能通过JSON对象的集合到Web服务,并与他们ASP.NET和C#服务器?

上处理

更新:

好吧,这是我的code。下面是个人类:

 公共类人
{
    公众人物()
    {
        //
        // TODO:添加构造逻辑这里
        //
    }    公众诠释序列
    {
        得到;
        组;
     }    公共字符串的firstName
    {
        得到;
        组;
     }
     公共字符串的lastName
     {
        得到;
        组;
     }}

这是我的JSON字符串:

  [{序列:1,名字:克里斯,姓氏:威斯布鲁克},
{序列:2,名字:sayyl,姓氏:威斯布鲁克}]

这里是code我用

  [的WebMethod]
    公共无效updatePeople(JSON字符串)
    {
        IList的<&人GT;人=
         新的JavaScriptSerializer()反序列化<&IList的LT;人>>(JSON);        //做东西...
    }


解决方案

我想通了。我没有我的包裹在JSON对象像ASP.NET AJAX需要。对于这个问题的将来的观众,所有的JSON对象必须与主对象被发送到web服务之前包裹。要做到这一点最简单的方法是在JavaScript中创建对象和使用类似json2.js到字符串化它。此外,如果使用的是ASMX Web服务,对象必须具有正确序列化__type属性。这样的一个示例可能是:

 变种人=新的对象;
person.firstName =克里斯
person.lastName =韦斯特布鲁克
person.seq = -1;
VAR数据=新对象;
data.p =人;
JSON.stringify(数据);

这将创建一个名为 P 的对象,将换一个人对象。这可以被链接到Web服务的一个参数 P 。键入的名单也同样提出,接受使用人,而不仅仅是一个单一的对象数组。我希望这可以帮助别人。

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 is not supported for deserialization of an array."

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#?

Update:

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

}  

And here is my JSON string:

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

And here is the code I'm using

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

        //do stuff...
    }

解决方案

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

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对象的名单,LT;类型&GT;与ASMX服务工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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