waq*_*lam 12 asp.net polymorphism json gson object-type
在.net 2.0中向asmx Web服务发送数据时,我们如何保留json字符串中的对象类型?
例如:
class A{
 string name;
}
class B : A {
 string address;
}
和网络方法:
[WebMethod]
public void PushJson(A obj){
  B b = (B) obj;
}
现在在上面的示例场景中,让我说,我发送{"obj":{"name":"waqas","address":"sweden"}}然后如何强制我的json字符串充当类B的类型,以便它可以被Web方法接受为类A的对象并进一步解析回类的对象B' 总之,如何在json中保留多态?
我注意到,System.InvalidCastException当我尝试执行这种模式时,编译器会抛出我
PS我注意到.net 在序列化为json时为复杂对象添加了__type.是否有可能我们可以包含此密钥来帮助.net自动解析具有正确类类型的json字符串?
任何帮助/建议都会有所帮助.
如果我们仔细观察wsdlasmx web服务,那么类继承父类的对象包含类似的东西<s:extension base="tns:ParentClassName">.我认为这个扩展部分是我可能需要转换为Json的东西.对此有何想法?
你在标题中提到了GSON,但我不确定它在这张图片中的位置.所以,我可能会出现错误的切线.但是,如果您只是要求让.NET反序列化您的JSON,是的,您可以使用__type参数.它必须是第一位的.
{"obj":{"__type":"B","name":"waqas","address":"sweden"}}
我能够在测试项目中使用它,但就像我说的那样,没有GSON参与其中.
编辑: 实际上,您可能还想看到另一个答案/sf/answers/756400081/,其中讨论了如何让GSON包含该__type参数.
编辑2: 我创建了一个新的.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","name":"waqas","address":"sweden"}}',
                    contentType: "application/json; charset=utf-8",
                    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参数.