WEBMETHOD不能删除对象{D:"" } [英] Webmethod can't remove Object { d: "" }

查看:297
本文介绍了WEBMETHOD不能删除对象{D:"" }的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做的是不是发送对象{D:{写到FileID:1213}} 发送{ 写到FileID:1213}

我目前的code:

 使用系统;
使用System.Web.Mvc;
使用System.Data这;
使用System.Data.SqlClient的;
使用System.Configuration;
使用的System.Web;
使用System.Web.Services;
使用System.Web.Script.Services;
使用System.Web.Script.Serialization;[ScriptService]
部分类testing_class:System.Web.UI.Page
{    保护无效的Page_Load(对象发件人,EventArgs的发送)
    {
        会话[写到FileID] =的Request.QueryString [写到FileID];    }    公共静态字符串returnJSON(对象o)
    {
        JS的JavaScriptSerializer =新的JavaScriptSerializer();
        返回js.Serialize(O);
    }    [的WebMethod]
    [ScriptMethod(UseHttpGet = TRUE,ResponseFormat = ResponseFormat.Json)
    公共静态字符串CurrentFile()
    {
        变种D = {新写到FileID =123};
        返回returnJSON(D);
    }
};


解决方案

Microsoft堆栈的Json序列化是pretty很多过时的,应该不惜一切代价避免。相反,你应该使用(和新的.NET Web栈已经是默认设置) Json.NET 实施

如果你没有安装它,你可以通过运行这样做就安装封装Newtonsoft.Json 在你的NuGet控制台窗口。另外,还要确保你是使用Newtonsoft.Json;

  [的WebMethod]
[ScriptMethod(UseHttpGet = TRUE,ResponseFormat = ResponseFormat.Json)
公共静态字符串CurrentFile()
{
    变种D = {新写到FileID =123};
    返回JsonConvert.SerializeObject(D);
}

What I want to do is instead of sending Object { d : "{"FileID":"1213"}" } send "{"FileID":"1213"}"

My current code:

using System;
using System.Web.Mvc;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;
using System.Web.Script.Serialization;

[ScriptService]
partial class testing_class : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        Session["FileID"] = Request.QueryString["FileID"];

    }

    public static string returnJSON(object o)
    {
        JavaScriptSerializer js = new JavaScriptSerializer();
        return js.Serialize(o);
    }

    [WebMethod]
    [ScriptMethod(UseHttpGet = true,ResponseFormat = ResponseFormat.Json)]
    public static string CurrentFile()
    {
        var d = new { FileID = "123" };
        return returnJSON(d);
    }


};

解决方案

Microsoft stack Json serializers are pretty much obsolete and should be avoided at all costs. Instead you should be using (and newer .NET Web stack already is by default) Json.NET implementation.

If you don't have it installed you can do so by running Install-Package Newtonsoft.Json in your NuGet console window. Also make sure you are using Newtonsoft.Json;.

[WebMethod]
[ScriptMethod(UseHttpGet = true,ResponseFormat = ResponseFormat.Json)]
public static string CurrentFile()
{
    var d = new { FileID = "123" };
    return JsonConvert.SerializeObject(d);
}

这篇关于WEBMETHOD不能删除对象{D:"" }的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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