【问题标题】:removing xml tags from json data从 json 数据中删除 xml 标签
【发布时间】:2013-01-25 11:16:48
【问题描述】:

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

【问题讨论】:

  • 看看this的问题,可能对你有帮助

标签: asp.net xml json web-services


【解决方案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)

结束子

【讨论】:

    【解决方案2】:

    您需要查看您如何请求数据并强制 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
        }
    });
    

    【讨论】:

    • 我的代码在下面,我使用字符串作为脚本方法。我不明白是什么问题。
    • 您正在返回 XML,因为您正在返回字符串。您需要请求 JSON 并让 .NET 处理您的对象的序列化。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-31
    • 2021-04-26
    • 2018-12-21
    • 2023-03-06
    • 1970-01-01
    • 2019-10-27
    • 1970-01-01
    相关资源
    最近更新 更多