从json数据中删除xml标签

问题描述:

我对json数据有疑问。我可以将数据库中的数据转换为asp.net web服务中的json数据,但它们带有xml标签。我需要从这些数据中删除字符串标签和xml信息。数据的外观:从json数据中删除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}] 
+1

看一看[这](http://*.com/questions/9288270/webservice-returns-json-data-但可以帮助你 –

你需要看你如何请求数据和力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 
    } 
}); 
+0

我的代码如下,我用字符串的脚本方法。我不明白是什么问题。 – pln

+0

因为你正在返回字符串,所以你得到了XML。您需要请求JSON并让.NET处理您的对象的序列化。 – Echilon

+0

感谢Echilon! – pln

进口System.Data

进口系统.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 

末级

公共功能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)> _ 

公用Sub GetProduct()

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

末次