service - ASP.NET JSON Web 服务响应格式

标签 service response json

我编写了一个简单的 Web 服务,它在 JSONText 中获取产品列表,它是字符串对象

网络服务代码如下

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;
using System.Runtime.Serialization.Json;
using System.IO;
using System.Text;

/// <summary>
/// Summary description for JsonWebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class JsonWebService : System.Web.Services.WebService 
{

    public JsonWebService () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string GetProductsJson(string prefix) 
    {
        List<Product> products = new List<Product>();
        if (prefix.Trim().Equals(string.Empty, StringComparison.OrdinalIgnoreCase))
        {
            products = ProductFacade.GetAllProducts();
        }
        else
        {
            products = ProductFacade.GetProducts(prefix);
        }
        //yourobject is your actula object (may be collection) you want to serialize to json
        DataContractJsonSerializer serializer = new DataContractJsonSerializer(products.GetType());
        //create a memory stream
        MemoryStream ms = new MemoryStream();
        //serialize the object to memory stream
        serializer.WriteObject(ms, products);
        //convert the serizlized object to string
        string jsonString = Encoding.Default.GetString(ms.ToArray());
        //close the memory stream
        ms.Close();
        return jsonString;
    }
}

现在它给了我如下的响应:

{"d":"[{\"ProductID\":1,\"ProductName\":\"Product 1\"},{\"ProductID\":2,\"ProductName\":\"Product 2\"},{\"ProductID\":3,\"ProductName\":\"Product 3\"},{\"ProductID\":4,\"ProductName\":\"Product 4\"},{\"ProductID\":5,\"ProductName\":\"Product 5\"},{\"ProductID\":6,\"ProductName\":\"Product 6\"},{\"ProductID\":7,\"ProductName\":\"Product 7\"},{\"ProductID\":8,\"ProductName\":\"Product 8\"},{\"ProductID\":9,\"ProductName\":\"Product 9\"},{\"ProductID\":10,\"ProductName\":\"Product 10\"}]"}

但我正在寻找低于输出

[{"ProductID":1,"ProductName":"Product 1"},{"ProductID":2,"ProductName":"Product 2"},{"ProductID":3,"ProductName":"Product 3"},{"ProductID":4,"ProductName":"Product 4"},{"ProductID":5,"ProductName":"Product 5"},{"ProductID":6,"ProductName":"Product 6 "},{"ProductID":7,"ProductName":"Product 7"},{"ProductID":8,"ProductName":"Product 8"},{"ProductID":9,"ProductName":"Product 9"},{"ProductID":10,"ProductName":"Product 10"}]

谁能告诉我什么是实际问题

谢谢

最佳答案

首先,出于安全原因,ASP.NET 3.5 发生了变化,Microsoft 在响应中添加了“d”。以下是来自 Encosia 的 Dave Ward 的链接,该链接讲述了您的所见:
A breaking change between versions of ASP.NET AJAX .他有几篇文章讨论了这一点,可以帮助您进一步处理 JSON 和 ASP.NET

关于service - ASP.NET JSON Web 服务响应格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1238672/

相关文章:

javascript - express /Node.js : Render custom javascript as response

Javascript - 删除 JSON 字符串中出现的 u',解析返回意外标记

windows - 即使使用同一用户进行服务,也无法通过 Windows 服务访问 UNC 路径

windows - 创建服务(SERVICE_ACCEPT_SESSIONCHANGE)

java - 应为 BEGIN_ARRAY,但在第 1 行第 5921 列路径 $.data[5].courier.data 处为 BEGIN_OBJECT

json - 将复杂的 json 解码为复杂的数据结构在一个子结构上失败

json - Elastic Beanstalk:错误:无法解析Dockerrun JSON文件:json:无效使用,string struct标签,试图将未报价的值解码为int

ubuntu - 在 upstart conf 文件中获取一个文件

java - 在加载另一个应用程序时运行我的应用程序

http - 403响应可以吗?