2013-01-25 140 views
0

我對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://stackoverflow.com/questions/9288270/webservice-returns-json-data-但可以幫助你 –

回答

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 
    } 
}); 
+0

我的代碼如下,我用字符串的腳本方法。我不明白是什麼問題。 – pln

+0

因爲你正在返回字符串,所以你得到了XML。您需要請求JSON並讓.NET處理您的對象的序列化。 – Echilon

+0

感謝Echilon! – pln

1

進口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) 

末次