ASP.NET JSON Web服务响应格式

His*_*cal 6 service json response

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

Web服务代码如下

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

现在它给我像下面的resoponse:

{"d":"[{\""ProductID \":1,\"ProductName \":\"产品1 \"},{\"ProductID \":2,\"ProductName \":\"产品2\\ "},{\"ProductID \":3,\"ProductName \":\"Product 3 \"},{\"ProductID \":4,\"ProductName \":\"Product 4 \"},{ \"ProductID \":5,\"ProductName \":\"Product 5 \"},{\"ProductID \":6,\"ProductName \":\"Product 6 \"},{\"ProductID\":7,\"ProductName \":\"产品7 \"},{\"ProductID \":8,\"ProductName \":\"产品8 \"},{\"ProductID \":9, \"ProductName \":\"产品9 \"},{\"ProductID \":10,\"ProductName \":\"产品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"}]

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

谢谢

ewr*_*kin 8

首先,出于安全原因,ASP.NET 3.5发生了变化,微软在响应中添加了"d".下面是来自Encosia的Dave Ward的链接,它讲述了你所看到的内容: ASP.NET AJAX版本之间的重大变化.他有几个帖子谈到这个可以帮助你进一步处理JSON和ASP.NET

  • 那是因为你返回一个字符串,所以它在响应中转义引号.尝试将您的退货类型更改为List <Product>并返回"products".您不需要自己序列化字符串,具有WebMethod属性的ASP.NET将为您处理. (2认同)