WebMethod自动将类对象作为JSON返回 [英] WebMethod returning class object as JSON automatically

查看:112
本文介绍了WebMethod自动将类对象作为JSON返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以解释一下ASP.NET如何处理WebMethods中从类对象到JSON对象的转换吗?

Can anyone explain me how ASP.NET handles the convertion from a class object to the JSON object in WebMethods?

例如,您具有以下WebMethod,该WebMethod返回Person对象:

For example, you have following WebMethod which returns a Person object:

    [WebMethod]
    public static Person GetPerson()
    {
        Person p = new Person() 
        {
            Id = 1,
            Name = "Test"
        };

        return p;
    }

在我调用WebMethod的jQuery中,我得到一个包含json对象之外的响应.

In my jQuery where i call the WebMethod I get a response which contains out of a json object.

ASP.NET是如何自动执行此操作的?它使用JavaScriptSerializer类吗?

How did ASP.NET do this automatcally? Does it uses the JavaScriptSerializer class?

您还会看到很多使用JSON转换器将您的类对象转换为json对象的示例.为什么是这样?是由于它使用的JavaScriptSerializer类及其不良性能还是...?

Also you see a lot of examples of using JSON converters to convert your class object to a json object. Why is this? Is it because of the JavaScriptSerializer class it uses and its bad performance or... ?

推荐答案

ASP.NET是如何自动执行此操作的?

How did ASP.NET do this automatically?

基本上,Web和WebMethod之间有一些代码来处理请求,弄清请求的内容,找到您的WebMethod并获取结果,然后根据请求标头中可接受的格式将其序列化回客户端.

Basically there's some code sitting between the web and the WebMethod that takes the request, figures out what it's requesting, finds your WebMethod and obtains the result, then serializes it back to the client based on the acceptable formats in the request header.

它使用JavaScriptSerializer类吗?

Does it uses the JavaScriptSerializer class?

可能是.我找不到任何表明它的东西.但是它不使用任何第三方库.由于这是内置的,因此是一个很好的假设.

Probably. I couldn't find anything out there that stated it. But it doesn't use any third party library. Since that's one is built in, that's a good assumption.

您还会看到很多使用JSON转换器进行转换的示例 您的类对象转换为json对象.为什么是这样?是因为 它使用的JavaScriptSerializer类及其性能不好还是...?

Also you see a lot of examples of using JSON converters to convert your class object to a json object. Why is this? Is it because of the JavaScriptSerializer class it uses and its bad performance or... ?

WebMethod技术可能非常挑剔,尽管接受标头,有时仍会拒绝返回JSON.解决该问题的一种方法是执行以下操作:

WebMethod technique can be very finicky and sometimes will refuse to return JSON, despite the accept headers. One way around that is to do something like this:

[WebMethod]
public static void GetPerson()
{
    Person p = new Person() 
    {
        Id = 1,
        Name = "Test"
    };
    HttpContext.Current.Response.ResponseType = "application/json";
    HttpContext.Current.Response.Write(JsonConvert.SerializeObject(p));
    HttpContext.Current.Response.End();
}

您失去了内容协商(除非您通过检查请求标头手动实现它),但是您可以更好地控制其序列化方式.

You lose content negotiation (unless you manually implement it by inspecting the request headers), but you get more control over how it's serialized.

这篇关于WebMethod自动将类对象作为JSON返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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