本文介绍了ASP.NET JSON Web服务响应格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have written one simple web service which get product list in JSONText which is string object

Web服务代码在下面

Web Service code is below

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

{"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":产品1"},{"ProductID":2,"ProductName":产品2"},{"ProductID":3,"ProductName":产品3},{"产品ID:4,"产品名称:"产品4},{"产品ID:5,"产品名称:"产品5},{"产品ID:6,"产品名称: 产品6"},{产品ID":7,产品名称":产品7"},{产品ID":8,产品名称":产品8"},{产品ID":9,产品名称" :产品9"},{产品ID":10,产品名称":产品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"}]

任何人都可以告诉我什么是实际问题

can any one tell me what is actual problem

谢谢

推荐答案

首先,出于安全原因,ASP.NET 3.5进行了更改,Microsoft在响应中添加了"d".以下是恩科西亚(Encosia)戴夫·沃德(Dave Ward)的链接,内容涉及您的见解: ASP版本之间的重大更改. NET AJAX .他有几篇关于此的文章,可以帮助您进一步处理JSON和ASP.NET

First there was a change with ASP.NET 3.5 for security reasons Microsoft added the "d" to the response. Below is a link from Dave Ward at the Encosia that talks about what your seeing:A breaking change between versions of ASP.NET AJAX. He has several posts that talks about this that can help you further with processing JSON and ASP.NET

这篇关于ASP.NET JSON Web服务响应格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-15 23:09