加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – 如何在Json中使用Gson将对象类型包含在asmx web服务

发布时间:2020-12-15 22:39:11 所属栏目:asp.Net 来源:网络整理
导读:当发送数据到.net 2.0中的asmx web服务时,我们如何保留json字符串中的对象类型? 例如: class A{ string name;}class B : A { string address;} 和web方法: [WebMethod]public void PushJson(A obj){ B b = (B) obj;} 现在在上面的示例场景中,让我说,我发
当发送数据到.net 2.0中的asmx web服务时,我们如何保留json字符串中的对象类型?

例如:

class A{
 string name;
}

class B : A {
 string address;
}

和web方法:

[WebMethod]
public void PushJson(A obj){
  B b = (B) obj;
}

现在在上面的示例场景中,让我说,我发送{“obj”:{“name”:“waqas”,“address”:“sweden”}}那么我如何强制我的json字符串作为类B类,所以它可以被Web方法接受为A类的对象,并进一步解析为B类的对象?总而言之,如何保存json中的多态性?

我注意到,当我尝试执行这样的模式时,编译器会抛出System.InvalidCastException异常

附:我注意到.net在序列化到json时为复杂对象添加__type.我们可以包括这个键来帮助.net自动解析具有正确类类型的json字符串吗?

任何帮助/建议将是有帮助的.

更新:

如果我们仔细观察到一个asmx web服务的wsdl,那么类继承父类的对象包含类似于< s:extension base =“tns:ParentClassName”>的对象.我认为这个扩展部分是我可能需要转换成Json的东西.有什么想法吗?

解决方法

你在标题中提到GSON,但我不确定这张照片在哪里播放.所以,我可能会错误的切线.但是,如果您只是要求.NET来反序列化JSON,那么可以使用__type参数.必须先来.
{"obj":{"__type":"B","name":"waqas","address":"sweden"}}

我能够在测试项目中工作,但像我说的,没有GSON参与.

编辑:
实际上,您可能还想看到另一个答案https://stackoverflow.com/a/10805715/1539015,谈到如何使GSON包含该__type参数.

EDIT2:
我创建了一个新的.NET网站.添加了一个类文件与您的A和B类(修改为使其公开):

public class A
{
    public string name;
}

public class B : A
{
    public string address;
}

然后,我添加了一个Web服务,让WebMethod在你的问题.我还包括一个GetJson方法.以下是代码:

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService {

    public WebService () {
    }

    [ScriptMethod(ResponseFormat = ResponseFormat.Json,UseHttpGet = true)]
    [WebMethod]
    public B GetJson()
    {
        return new B() { address = "addr",name = "nm" };
    }

    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    [WebMethod]
    public string PushJson(A obj)
    {
        B b = (B)obj;
        return b.address;
    }
}

然后,我使用jQuery编辑默认页面来调用Web方法:

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <p>
        <div id="GetResult">Click here for the result of getting json.</div>    
        <div id="PushResult">Click here for the result of pushing json.</div>    
    </p>
    <script type="text/javascript">
        $(document).ready(function () {
            // Add the page method call as an onclick handler for the div.
            $("#GetResult").click(function () {
                $.ajax({
                    type: "GET",url: "WebService.asmx/GetJson",contentType: "application/json; charset=utf-8",success: function (msg) {
                        // Replace the div's content with the page method's return.
                        $("#GetResult").text(msg.d.name);
                    }
                });
            });
            $("#PushResult").click(function () {
                $.ajax({
                    type: "POST",url: "WebService.asmx/PushJson",data: '{"obj":{"__type":"B","address":"sweden"}}',dataType: "json",success: function (msg) {
                        // Replace the div's content with the page method's return.
                        $("#PushResult").text(msg.d);
                    }
                });
            });
        });  
    </script>
</asp:Content>

如果在PushJson的webservice方法中放置一个断点,可以看到所创建的对象的类型为B,并且运行的对象也可以转换为类型B并使用.

这里没有GSON,但我相信我链接的其他帖子应该显示如何让GSON生成__type参数.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读