从json数据中删除xml标记

时间:2013-01-25 11:16:49

标签: asp.net xml json web-services

我对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}]

2 个答案:

答案 0 :(得分:1)

Imports System.Data

导入System.Data.SqlClient

Imports System.Collections.Generic

导入System.Web.Script.Serialization

公共类产品

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

结束班

Public Function GetProductJSon()As String

    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)> _

Public Sub GetProduct()

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

End Sub

答案 1 :(得分:0)

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

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

[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
    }
});