0

我对 json 数据有疑问。我可以在 asp.net Web 服务中将数据从数据库转换为 json 数据,但它们带有 xml 标签。我需要从这些数据中删除字符串标签和 xml 信息。数据的外观是:

?xml version="1.0" encoding="utf-8" ?

string xmlns="http://tempuri.org/"

[{"ID":10,"Duration":24235},{"ID":21,"Duration":9034},{"ID":12,"Duration":13681},{"ID":1,"Duration":23053},{"ID":13,"Duration":22863},{"ID":22,"Duration":57163}]
4

2 回答 2

1

导入 System.Data

导入 System.Data.SqlClient

导入 System.Collections.Generic

导入 System.Web.Script.Serialization

公共类产品

    Public ProductID As Integer
    Public ProductName As String
    Public VendorID As Integer
    Public GroupID As Integer

结束类

公共函数 GetProductJSON() 作为字符串

    Dim ls As New List(Of Product)
    Dim Temp As String = ""
    Dim js As New JavaScriptSerializer
    Dim json As String
    Try
        Using cn As New SqlConnection(Helper.ConnectionString)
            Using cm As SqlCommand = cn.CreateCommand()
                cm.CommandType = CommandType.StoredProcedure
                cm.CommandText = "GetProdct"
                cm.Connection.Open()
                Dim da As SqlDataAdapter = New SqlDataAdapter(cm)
                Dim dt As DataTable = New DataTable
                Dim ds As DataSet = New DataSet
                da.Fill(ds, "Product")
                Dim clsProduct As New Product
                dt = ds.Tables(0)
                For i = 0 To dt.Rows.Count - 1
                    clsProduct.ProductID = dt.Rows(i)("ProductID")
                    clsProduct.ProductName = dt.Rows(i)("ProductName")
                    clsProduct.VendorID = dt.Rows(i)("VendorID")
                    clsProduct.GropID = dt.Rows(i)("GropID")

                    ls.Add(clsProduct)
                Next
            End Using
        End Using
        json = js.Serialize(ls)
        Return json
    Catch ex As Exception


    End Try

结束功能

<WebMethod()> _

<ScriptMethod(ResponseFormat:=ResponseFormat.Json, XmlSerializeString:=False)> _

公共子 GetProduct()

    Dim str As String = GetProductJSon()
    Context.Response.Clear()
    Context.Response.ContentType = "text/json"
    Context.Response.Charset = "utf-8"
    Context.Response.Write(str)

结束子

于 2016-06-09T19:30:12.257 回答
0

您需要查看如何请求数据并强制 ASP.NET 返回 JSON(这可能很繁琐)。

使用以下方法装饰您的 Web 服务方法:

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public List<string> GetData() {

然后确保设置内容类型并通过 POST 请求数据:

$.ajax({
    type: "POST",
    url: "/YourService.asmx/GetData",
    data: markers,
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(data){
        // your actual data will be in data.d
    },
    failure: function(errMsg) {
        // show error
    }
});
于 2013-01-25T12:42:06.483 回答