Deserializing JSON objects as List not worki

2019-01-14 07:12发布

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

5条回答
迷人小祖宗
2楼-- · 2019-01-14 07:21

SERVER SIDE

[WebMethod]
public void updatePeople(object json)

CLIENT SIDE

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

var str = "{'json':'" + JSON.stringify(person) + "'}";
查看更多
再贱就再见
3楼-- · 2019-01-14 07:23

Or even simpler, when you are doing the $.ajax(...) use

data:"{\"key\":"+JSON.stringify(json_array)+"}",

and then on the other side of the code, make your function use the parameter "object key"

[WebMethod]
public static return_type myfunction(object key){...}
查看更多
唯我独甜
4楼-- · 2019-01-14 07:24

Could you show the JSON string you are trying to deserialize and the way you are using the Deserialize method? The following works fine:

using System;
using System.Collections.Generic;
using System.Web.Script.Serialization;

namespace Test
{
    class Program
    {
        class Person 
        {
            public int SequenceNumber { get; set; }
            public string FirstName { get; set; }
            public string LastName { get; set; }
        }

        public static void Main() 
        {
            string json = "[{\"SequenceNumber\":1,\"FirstName\":\"FN1\",\"LastName\":\"LN1\"},{\"SequenceNumber\":2,\"FirstName\":\"FN2\",\"LastName\":\"LN2\"}]";
            IList<Person> persons = new JavaScriptSerializer()
                .Deserialize<IList<Person>>(json);
            Console.WriteLine(persons.Count);
        }
    }
}
查看更多
We Are One
5楼-- · 2019-01-14 07:28

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.

查看更多
太酷不给撩
6楼-- · 2019-01-14 07:28

I think the problem is what type you have to deserialize. You are trying to deserialize type

IList

but you should try to deserialize just

List

Since interface can not be instantiated this might is the root problem.

查看更多
登录 后发表回答